Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test using wildcards whether a file exists in Inno Setup

Tags:

inno-setup

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
like image 861
Andy Groom Avatar asked Apr 17 '14 11:04

Andy Groom


1 Answers

Currently, there is no function that would support wildcards for checking whether a certain file exists or not. That's because both FileExists and FileSearch functions internally use NewFileExists 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:

[Code]
function FileExistsWildcard(const FileName: string): Boolean;
var
  FindRec: TFindRec;
begin
  Result := False;
  if FindFirst(FileName, FindRec) then
  try
    Result := FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0;
  finally
    FindClose(FindRec);
  end;
end;

Its usage is same as of FileExists function, just you can use wildcards for the search like describes the MSDN reference for the lpFileName parameter of the FindFirstFile function. So, to check if there's a file with txt extension in the C:\Folder directory you can call the above function this way:

if FileExistsWildcard('C:\Folder\*.txt') then
  MsgBox('There is a *.txt file in the C:\Folder\', mbInformation, MB_OK);

Of course the file name to be searched may contain a partial name of the file, like e.g.:

if FileExistsWildcard('C:\Folder\File*.txt') then
  MsgBox('There is a File*.txt file in the C:\Folder\', mbInformation, MB_OK);

Such pattern will match files like e.g. C:\Folder\File12345.txt.

like image 183
TLama Avatar answered Nov 16 '22 05:11

TLama