PC Review


Reply
Thread Tools Rate Thread

Checking for file existence in a directory...

 
 
Ben
Guest
Posts: n/a
 
      4th Jun 2009
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
 
Reply With Quote
 
 
 
 
Graham Mandeno
Guest
Posts: n/a
 
      4th Jun 2009
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

--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

"Ben" <(E-Mail Removed)> wrote in message
news:ec%(E-Mail Removed)...
> 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



 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      4th Jun 2009
Hi Graham,

That's exactly what I needed. Thanks so much.

Ben


On 6/4/2009 6:14 PM, Graham Mandeno wrote:
> 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
>


 
Reply With Quote
 
Stuart McCall
Guest
Posts: n/a
 
      4th Jun 2009
> 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.


 
Reply With Quote
 
Graham Mandeno
Guest
Posts: n/a
 
      5th Jun 2009
Quite right! Thanks, Stuart - sorry for the brain fade :-)

--
Cheers,
Graham

"Stuart McCall" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>> 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking for file existence John Microsoft VB .NET 5 22nd Sep 2005 08:04 PM
Checking for existence of file before issuing Kill =?Utf-8?B?QnJpYW4=?= Microsoft Access VBA Modules 1 29th Oct 2004 09:12 PM
Vba - Checking existence of file ajliaks Microsoft Excel Programming 1 11th Aug 2004 06:43 PM
Checking for a key's existence Simon Microsoft Windows 2000 Registry 3 18th Feb 2004 06:12 PM
Checking for a key's existence Simon Microsoft Windows 2000 Registry Archive 0 18th Feb 2004 03:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:18 PM.