Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wildcard in a filename.

Status
Not open for further replies.

PALman

Technical User
May 15, 2002
160
GB
I have been looking through the posts for info on wildcards within a file name.
I have a number of documents (Excel,Word and PDF files)which I need to open from a command button.
They all have one thing in common in that they are named with firstly a Job Number followed by a hyphen and after that a unique combination of alphanumeric charachters. So I have something like this...
JobNo = textbox(11).txt
Filename = JobNo & "-" & "PO452345.PDF"
or
Filename = JobNo & "-" & "MPI.xls"
I just cannot get the syntax correct for a wild card (%) to replace the part after the "-"
Most of what I have read here uses LIKE but I wonder if it is necessary in this simple file name.
Any help would be much appreciated.
 
I think you use the asterisk "*"

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thanks Error7
I shall try code below when I return to machine in a few hours...
Filename = JobNo & "-" & "*"
Cheers!
 
First, wouldn't be easier to skip one & sign:

Filename = JobNo & "-PO452345.PDF"
or
Filename = JobNo & "-MPI.xls"

Second, I don't think you can use wildcard in a filename when you try to open a file. Syntax is for a specific file, not "Open any file with abc*.pdf in a filename"

Just my $0.02

---- Andy
 
Quite right Andy, I should have clarified that the wildcard will only give you a DIR of relevant files which you can put into an array or Listbox and then open from there.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Hi Guys,
I think what you are both saying is correct. "*" does not work.
The problem I have is trying to load up a specific file which could be named...
222555-CUST-PO.pdf or 222555-PO.pdf or 222555-PO-45000675.pdf The only constant would be 222555 (the JobNo) the - (hyphen) and I could probably make the PO a constant (stands for Purchase Order) but many of these files are named with the PO number the last part 45000675.
It looks as if I shall have to try and get everyone to be consistant in naming these files and then I could simply use something like...
Filename = JobNo & "-" & "CUST-PO.pdf"
I was just hoping there might be a way to overcome this without having to rename hundreds off files.
Thanks
 
Assuming that what you are saying is that there is only one unique file for each PO in the folder, (i.e there is 222555-CUST-PO.pdf or 222555-PO.pdf or 222555-PO-45000675.pdf, but not 222555-CUST-PO.pdf and 222555-PO-45000675.pdf) then:

Filename = JobNo & "-" & "*.pdf"
UniqueFile= Dir(Filename)
 
Hi Strongm,
Tried your code which I thought would work but it cannot find file with message... can't find "222555-*.pdf"
Thanks
 



>cannot find file with message... can't find "222555-*.pdf"

Are you trying to open a file named "222555-*.pdf" ??!!

That will not work! Imagine 500 files matching that pattern are found. Should all open now?

However, * works with DIR(), as shown by strongm, (which supports the use of single and multiple wildcards - * and ?), will work to find matching files!

It then returns the full file name of one of possible many files which matched the pattern, if one is found at all, otherwise it returns an empty string!

Once a file, or one of the matching files, is found, use the actual full file name returned by DIR() in the open statement.

If more than one is in the same folder, you will have to decide which one to open.

 
Hi SBerthold
I am trying to open only one file in one folder but I do not know how it would be named. As I said typical file names could be...
222555-PO-45000675.pdf or
222555-PO-45000777.pdf or
222555-Cust-PO.pdf
Thanks
 

Then, use the return value from the DIR() method to open the file with as strongm has shown (open the file found and held in the UniqueFile variable, or pass it up if it is empty).

And your next post in this thread should be:
"yes, this works and I gave strongm a star"
or
"Tonight I'm going early to bed
 
Sorry SBerthold
Will try tomorrow, however I am not surre what you mean as I am new to VB6.
Any further help explanation appreciated
Thanks
 

That is what I thought...and you are probably looking for a complete solution, code and all, or someone to walk you through it step by step (ref. to previous threads)?

That may not always happen here. Probably shouldn't, when pertaining to the very basics.

Obviously, the first step is then is read some beginner books, look at the tutorials, and check the vb help files and examples using specific keywords (DIR).
Most beginner books/online tutorials show how to find files and open them.

Maybe someone else has a different opinion.
 
Sorry SBerthold
I am new to VB6 but with a lot of experience and applications written in VBA for Access.
This application I am working on is at an advanced stage however its possible I missed this in the basics on how to use wildcards in calling up a file name. Its also interesting to here two programmers here say you can't use widcards in a filename???
 
PAlman
What you were told above is correct - i don't think you are executing it correctly

IF you ran

dim fpath as string '''the directory path to your files
dim flname as string ''filename

fpath = C:\temp" '''Substitutre your directory name
Flname = Dir(fpath & "\222555-*.*)
do while flname <> ""
debug.print flname
flname = dir()
loop

you will get a listing of every file in the fpath folder that starts with 222555-

 
If you're having problems with understandingstrongm's simple code and SBerthold's added clarification, try reading VB help on the Dir function. That will explain in detail how you can use the Dir function with wildcards. You then use the result from that function as the file to open.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Thanks yacyac (and johnwm)
I understand what you have told me, but what follows your code to enable me to load and display that one single file of which I do not know how it would be named except for the begining "222555-"
To reiterate...
I am trying to OPEN only ONE file in one folder not to actually list the files begining with "222555". It may be the only file in the folder but I do not know how it would be named. As I said typical file names for this one file could be...
222555-PO-45000675.pdf or
222555-PO-45000777.pdf or
222555-Cust-PO.pdf
Thanks for your help and sorry if I am missing something. I am still looking around for help on this simple issue.
 
Yes, you are missing something. Given the assumptions I indicated in my first post, the example I gave you will return the unique filename (and hence the obscure name I gave to the variable so as to confuse the issue...). So all you have to do is to use UniqueFile as the name for opening the file. e.g.


Filename = JobNo & "-" & "*.pdf"
UniqueFile= Dir(Filename)

hFile=FreeFile
Open UniqueFile for Input as hFile

(which has been specifically explained in words by both SBerthold and johnwm, and was also alluded to by Error7)

>two programmers here say you can't use widcards in a filename??

Neither of them said that. Please read what they wrote carefully

 
Does PALman needs an introduction to the "Shell" function as well?

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
I'm not sure I'm clear on one thing. Palman, how do you want to proceed if more than one file in a folder fits the wildcard specification? Do you want to select the right one manually?

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top