Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FileExists with wildcard *

1,192 views
Skip to first unread message

Ganshorn

unread,
Jan 10, 2008, 11:11:07 AM1/10/08
to
Hi,

I have problems by using the wildcard '*' in the filename for FileExists in
Delphi 2006.

Follwing source find the file 'c:\temp\fileex\test3a.txt' when compiled with
Delphi 6.
When I compile the same source with Delphi 2006, the file is not found.

if FileExists('c:\temp\fileex\test3*.txt') then
Label1.Caption := 'found'
else
Label1.Caption := 'not found';


If I use no wildcard and call FileExists('c:\temp\fileex\test3a.txt') , the
file is found with Delphi 2006.

Thanks in advance
Ganshorn


Finn Tolderlund

unread,
Jan 10, 2008, 11:16:20 AM1/10/08
to
The problem is that you think that FileExists can use wildcards.
It doesn't and never did.
--
Finn Tolderlund


"Ganshorn" <r...@ganshorn.de> skrev i meddelelsen
news:4786...@newsgroups.borland.com...

Remy Lebeau (TeamB)

unread,
Jan 10, 2008, 1:11:50 PM1/10/08
to

"Finn Tolderlund" <n...@spam.com> wrote in message
news:4786...@newsgroups.borland.com...

> The problem is that you think that FileExists can use
> wildcards. It doesn't and never did.

Well, actually it used to, but only as a side effect of FileAge()'s
implementation. FileExists() was never intended to be used with wildcards,
I agree with that. See my other reply.


Gambit


Remy Lebeau (TeamB)

unread,
Jan 10, 2008, 1:10:35 PM1/10/08
to

"Ganshorn" <r...@ganshorn.de> wrote in message
news:4786...@newsgroups.borland.com...

> I have problems by using the wildcard '*' in the filename for
> FileExists in Delphi 2006.

FileExists() does not support wildcards in D2006. You need to use
FindFirst() for that.

> Follwing source find the file 'c:\temp\fileex\test3a.txt' when
> compiled with Delphi 6.

In D6 and earlier, FileExists() called FileAge(), which called the Win32 API
FindFirstFile() function. FindFirstFile() (which FileFirst() also uses)
supports wildcards. FileAge() would return the age of the first file that
matches the wildcard. If found, then FileExists() could return True.

This was not proper behavior for a FileExists() type of function to
implement (especially considering that FileAge() can fail on existing files
that have certain problematic timestamps). FileExists() is meant for
checking for a single file only, not a wildcard list of multiple files.
Also, checking a file's age is not the proper way to check for a file's
existance anyway. So in a later Delphi version, FileExists() was re-written
to use the Win32 API GetFileAttributes() function instead. This also
matches the implementation of DirectoryExists(), which has always used
GetFileAttributes(). GetFileAttributes() does not support wildcards,
however.

> When I compile the same source with Delphi 2006, the file is not found.

It is not supposed to, and you should never have used wildcards with
FileExists() in the first place anyway.


Gambit


Ganshorn

unread,
Jan 11, 2008, 2:27:16 AM1/11/08
to
Thank you Finn.

Ganshorn


Ganshorn

unread,
Jan 11, 2008, 2:24:43 AM1/11/08
to
Thank you very much Gambit for your detailed answer.

Ganshorn


HeartWare

unread,
Jan 11, 2008, 5:33:18 AM1/11/08
to
On Jan 10, 5:11 pm, "Ganshorn" <r...@ganshorn.de> wrote:

> I have problems by using the wildcard '*' in the filename for FileExists in
> Delphi 2006.

Like others have said, don't use FileExists with file masks. Use this
one instead:

FUNCTION MaskExists(CONST Name : STRING) : BOOLEAN;
VAR
SR : SearchRec;

BEGIN
FindFirst(Name,$27,SR);
Result:=(DosError=0) AND (SR.ATTR AND $18=$00);
IF DosError=0 THEN FindClose(SR)
END;

0 new messages