Is it possible to use FileExists
or FileSearch
(or any other Pascal function) to determine whether a file pattern exists in a given folder?
Eg:
if (FileExists('c:\folder\*.txt') = True) then
Is it possible to use FileExists
or FileSearch
(or any other Pascal function) to determine whether a file pattern exists in a given folder?
Eg:
if (FileExists('c:\folder\*.txt') = True) then
Currently, there is no function that would support wildcards for checking whether a certain file exists or not. That's because both
FileExists
andFileSearch
functions internally useNewFileExists
function which, as the comment in the source code states, doesn't support wildcards.Fortunately, there is the
FindFirst
which supports wildcards, so you can write a function like follows for your task:Its usage is same as of
FileExists
function, just you can use wildcards for the search like describes the MSDN reference for thelpFileName
parameter of theFindFirstFile
function. So, to check if there's a file withtxt
extension in theC:\Folder
directory you can call the above function this way:Of course the file name to be searched may contain a partial name of the file, like e.g.:
Such pattern will match files like e.g.
C:\Folder\File12345.txt
.