幸运的是,有 FindFirst 支持通配符,因此您可以为任务编写如下函数: [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; 其用法与 FileExists 函数的用法相同,只是您可以使用通配符进行搜索,例如描述 FindFirstFile 函数.因此,要检查C:\Folder目录中是否存在扩展名为txt的文件,您可以通过以下方式调用上述函数: if FileExistsWildcard('C:\Folder\*.txt') then MsgBox('There is a *.txt file in the C:\Folder\', mbInformation, MB_OK); 当然要搜索的文件名可能包含文件的部分名称,例如: if FileExistsWildcard('C:\Folder\File*.txt') then MsgBox('There is a File*.txt file in the C:\Folder\', mbInformation, MB_OK); 这种模式将匹配文件,例如C:\Folder\File12345.txt.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 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. 这篇关于如何使用通配符测试Inno Setup中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 16:36