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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a file with an unknown name...possible? 1

Status
Not open for further replies.

CHeighlund

Programmer
Jun 11, 2007
163
US
I'm working with a program that's supposed to read a flat file in, then parse it for output purposes. I know how you read a file in normally, like this:
Code:
  AssignFile(InFile, '[Path]\[to]\[Filename].[ext]');
  Reset(InFile);

The problem here is that I'm not sure I can count on the name of the file I'm reading in being static; if I understand correctly, it's being created by another program that I don't have access to, and the filenames are identified in part by the file's date of creation. (Ex: FOO_102907.txt, BAR_020205.txt, etc.) Is there some way to use a wildcard character in an AssignFile command's second parameter so I can catch anything with the appropriate extension? The extension is an in-house four character combination I can't recall having seen anywhere else before, so I'd assume it's unique enough. And I can guarantee that there will only be one file with the appropriate extension in the target directory I'm trying to read from, I simply can't guarantee the exact name of the file. The problem is, when I've tried using the '*' wildcard, my program throws errors. Is there some other way to pick up a file with a non-static name, or do I need to try and gain access to the original program that creates the file for me in the first place?
 
You should find that FindFirst function will provide the wildcard access to the file you want.

Something like this should work:
Code:
var
  tsr: TSearchRec;
...
begin
...
  if FindFirst( path + '???_??????.txt', 0, tsr ) = 0 then begin
    filename := path + tsr.name;
    ProcessFile( filename );    // Your code to process file
  end;
  FindClose( tsr );
...
The ? is a wildcard representing a single character. A * is the wildcard to represent more than one character.

Andrew
Hampshire, UK
 
This does appear to be doing what I needed done. I thank you for your assistance.
 
Would '?' wildcard work in a dospromt too?
I'm at work now, and wont get near any regular computer in a while. (Using these slimclients, ugly as hell!)
 
>Would '?' wildcard work in a dospromt too?

Yes. I presume DOS origins is the reason these characters are used this way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top