kkarnez
asked on
check for file
hello,
can anyone tell me, how can check if a file exists?
thanks
can anyone tell me, how can check if a file exists?
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Both of the above methods work well.
If you are only checking to see if the file exists the first method is faster than creating the scripting object then dropping it. If you are going to do a lot of file stuff then use the second method and keep the file system object around in your program so you don't need to keep recreating it.
If you are going to read from the file you can also just open it and check for an error (this is the fastest way if you are going to use the file when it exists)
on error resume next
open "yourfile" for input as #1
if err=0 then
'
' your code goes here
'
close #1
end if
If you are only checking to see if the file exists the first method is faster than creating the scripting object then dropping it. If you are going to do a lot of file stuff then use the second method and keep the file system object around in your program so you don't need to keep recreating it.
If you are going to read from the file you can also just open it and check for an error (this is the fastest way if you are going to use the file when it exists)
on error resume next
open "yourfile" for input as #1
if err=0 then
'
' your code goes here
'
close #1
end if
Set fso = CreateObject("Scripting.Fi
If (fso.FileExists(filespec))
debug.print filespec & " exists."
Else
debug.print filespec & " doesn't exist."
End If