-
I actually had this post in the General forum thinking more people were gonna see it, however, no response.
Oh wells, here it goes again...
Is the FileExists method of the FileSystemObject the only way to check if a file exists? I heard that using the FileSystemObject uses a lot of system resources. Any feedback would be greatly appreciated. Thanks.
ttlai:cool:
-
I would think that using the fileexists method is the only way, if not then any other way is going to be even slower.
-
You can use the Dir function.
Used only on its own it is faster than the filesystemobject because the filesystemobject must be created first.
But if you create the filesystemobject once, and then use the fileexists method in a loop it is faster.
In the speed comparison below I am creating the filesystemobject everytime I need it.
So if you only want to do a few files, use Dir(), but if you want to do loads of files, create the filesystemobject, and then use its fileexists method in a loop.
Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim timeMethod1 As Long
Dim timeMethod2 As Long
Dim i As Long
Dim fs As Object
Private Sub Form_Load()
timeMethod1 = GetTickCount()
For i = 0 To 10000
Set fs = CreateObject("Scripting.FileSystemObject")
If (fs.FileExists("c:\command.com")) Then
End If
Next i
timeMethod1 = GetTickCount() - timeMethod1
timeMethod2 = GetTickCount()
For i = 0 To 10000
If (Dir("c:\command.com") <> "") Then
End If
Next i
timeMethod2 = GetTickCount() - timeMethod2
MsgBox "Times Taken : " & vbCrLf & vbCrLf & "FileSystemObject : " & timeMethod1 & vbCrLf & "Dir Function : " & timeMethod2
End Sub
-
Code:
Dim Fileexists as boolean
If len(dir$("C:\Path\File.exe")) <> 0 then Fileexists = true
The above looks at the length of the file. Using the Dir function, but as Jamie's put, if there are a lot of files to check, use the FileSystemObject. :)
-
doo bee doo bee doo im not being followed at all ... doo bee doo .... ;)
-
Just that the LEN(DIR$( option's faster than the Dir( option ... ;)
-
If the file exists it is :)
-
thanks plenderj
i only need to check one file so i'm gonna go with the Dir() function
thanks again
ttlai
-