Checking for file existence in a directory...

B

Ben

Hi all,

I have a bunch of files in a directory and each are preceded by either a
two character or three character prefix code to the file name. I have a
table that contains these code, I obtained a record set of these codes
by running a select query.

Question:
I would like to step through the recordset and compare each record (ie,
the prefix code) and check to see if there in the directory, exists a
file with the same prefix code. How do I check the directory for the
existence of the file based on the prefix code in the recordset?

Thanks,

Ben
 
G

Graham Mandeno

Hi Ben

The Dir() function will return the full path name of a file if it exists,
otherwise it will return an empty string. It also works with wildcards, so
it can be used for your purpose to check for files whose names start with a
given string.

So, let's say you have a string variable "strFolder" that contains the full
folder path, including the trailing backslash, and you are loopng through a
recordset "rs" with a field named "Prefix". You can use code like this:

If Len(Dir(strFolder & rs!Prefix & "*") > 0 Then
' file starting with prefix exists
Else
' file starting with prefix does not exist
End If
 
S

Stuart McCall

The Dir() function will return the full path name of a file if it exists

I hate to contradict MVP's and you may think this is a little picky, but
that statement is not true. The dir function returns the file name of any
matching file, not the full path. So if you need to use the full path to the
file later in your code you must provide it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top