• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

check for file

hello,

can anyone tell me, how can check if a file exists?

thanks
0
kkarnez
Asked:
kkarnez
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
if dir("yourfile") = empty then
  'file does not exist
else
  'file does exist
end if

CHeers
0
 
rpaiCommented:
Dim fso as Object

Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
    debug.print filespec & " exists."
Else
    debug.print filespec & " doesn't exist."
End If
 
0
 
schworakCommented:
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now