Find out if file exists

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi. I know this is probably a simple question, but I'm befuddled as to the
correct way to go about it. I need to copy a file from one location to
another, but if the file does not exist, skip it and move on to the next one.
I've tried the "FileSystemObject.FileExists" method, but it keeps telling me
"Object Required" and I don't know what object it's looking for. I know I've
seen code along the lines of "If Exists "File" Then (code) Else End If", but
I'm not sure that it was Access code I saw or something else.
 
Dir() might do the job.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

in message
news:[email protected]...
 
Hi. I know this is probably a simple question, but I'm befuddled as to
the correct way to go about it. I need to copy a file from one
location to another, but if the file does not exist, skip it and move
on to the next one. I've tried the "FileSystemObject.FileExists"
method, but it keeps telling me "Object Required" and I don't know
what object it's looking for. I know I've seen code along the lines of
"If Exists "File" Then (code) Else End If", but I'm not sure that it
was Access code I saw or something else.

Before you use a FileSystemObject object, you need to instantiate it like
this;

Set fso = CreateObject("Scripting.FileSystemObject")

or

Set fso = New FileSystemObject ' if you've set a reference.


In any case, you really don't need it because Dir() will find the file
for you:

If Len(Dir(sourceFileName))>0 Then
' it exists, do the copy
FileCopy sourceFileName, destFileName, etc, etc.


Hope that helps


Tim F
 
That worked perfectly, thanks guys!

Tim Ferguson said:
Before you use a FileSystemObject object, you need to instantiate it like
this;

Set fso = CreateObject("Scripting.FileSystemObject")

or

Set fso = New FileSystemObject ' if you've set a reference.


In any case, you really don't need it because Dir() will find the file
for you:

If Len(Dir(sourceFileName))>0 Then
' it exists, do the copy
FileCopy sourceFileName, destFileName, etc, etc.


Hope that helps


Tim F
 
Back
Top