Problem trying to determine if a file exists using wildcard

  • Thread starter Thread starter Dan W
  • Start date Start date
D

Dan W

Hi there. I am parsing a text file and as I parse each line I need
to go out to two different directories and check to see if any files
beginning with the string I just parsed exist. For example... if the
first line in my text file is AS1501 then I need to check Dir1 and see
if there are any files that begin with AS1501 then I need to go to
Dir2 and check there also. I am trying to check using the following
code...

If File.Exists(Dir1 + "\" + subDir + "\" + pString + "*.pdf") Then
Do some stuff
End If

I used a messagebox to verify that the string that's being built does
point to the correct place and I've verified that the .pdf file I am
trying to check for does exist. However, when I put this IF THEN
statement in it never finds the file. I assume that this is because
it is seeing the asterisk as a literal asterisk rather than a wildcard
symbol. Just wondering if there is any way around this.

I'm new to vb.net and any help would be greatly appreciated.

Thanks!
 
Dan W said:
Hi there. I am parsing a text file and as I parse each line I need
to go out to two different directories and check to see if any files
beginning with the st
ringIjustparsedexist.Forexample...ifthe first line in my text file
is AS1501 then I need to check Dir1 and see if there are any files
that begin with AS1501 then I need to go to Dir2 and check there
also. I am trying to check using the following code...

If File.Exists(Dir1 + "\" + subDir + "\" + pString + "*.pdf") Then
Do some stuff
End If

The wildcard '*' is not a valid char in a file name.


Armin
 
One thing you could do, not sure if it is the best way or not, is to
use System.IO.Directory.GetFiles(). It takes the path to search and
the search pattern and returns a string() containing all the files that
match. So, you could do Directory.GetFiles(whateverpath, "*.pdf") then
check the return value.
 
Thank you very much. Using this method seems to have done the trick.
Thanks again.

Cheers!
 

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

Back
Top