Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi : how to check if a file exists (path over 255 characters)

Tags:

winapi

delphi

I need to make my delphi app able to check if a file copied using Robocopy is there or not when its path exceeds 255 characters. I have tried the usual "If FileExists(MyFile) then ... " but it always returns "false" even if the file is there.

I also tried to get the file's date but I get 1899/12/30 which can be considered as an empty date.

A File search does not return anything either.

like image 241
Fab Avatar asked Jun 01 '13 15:06

Fab


1 Answers

Prefix the file name with \\?\ to enable extended-length path parsing. For example you would write

if FileExists('\\?\'+FileName) then
  ....

Note that this will only work if you are calling the Unicode versions of the Win32 API functions. So if you use a Unicode Delphi then this will do the job. Otherwise you'll have to roll your own version of FileExists that calls Unicode versions of the API functions.

These issues are discussed in great length over on MSDN: Naming Files, Paths, and Namespaces.

like image 98
David Heffernan Avatar answered Oct 21 '22 05:10

David Heffernan