Find Long (> 255) File Names

It has a folder with more than 100 files. But all file and folder names with incorrect encoding names (UTF-> ANSI).

"C:\...\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\ІµЂ‚ѕ»µ‚є°\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\ІµЂ‚ѕ»µ‚є°\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\ІµЂ‚ѕ»µ‚є°\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\ІµЂ‚ѕ»µ‚є°\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\ІµЂ‚ѕ»µ‚є°\"ѕЃґ°‡°-›µ‡µ±Ѕ єѕЂїѓЃ\..." 

The regular function is Utf8ToAnsi finxing it, but FindFirst cannot search folders with names longer than 255 characters.

This gives me a total of 70/100 files.

+7
long-integer find filenames delphi
source share
2 answers

FindFirst wraps the Win32 FindFirstFile API function, and the Unicode version of this function can search for paths up to 32,767 characters long if you add \\?\ To the path you are passing, for example \\?\C:\Folder\Folder\* .

Since Delphi 2009 and new call Unicode functions for you, you can just use FindFirst and collaborate there. For Delphi 2007 and earlier (ANSI versions) you need to directly call FindFirstFile/FindNextFile/FindClose from Windows.pas . For more information, check File Name in the platform SDK section.

Note that using \\?\ Disables the various bits of the path processing, so make sure that this is the full path without any ".". or '..'. You can use the same trick to open file streams, rename or copy files with longer paths.

The Explorer does not support this, so you still need to limit them to no more than MAX_PATH characters for things like SHFileOperation (to be deleted to the trash) or ShellExecute . In many cases, you can work around the problem by going to DOS 8.3 names instead of long ones. FindFirst TSearchRec does not expose short names, but the FindFirstFile TWin32FindData structure works like cAlternateFileName .

+13
source share

Change the current directory ( ChDir ) to the deepest that you know about, and then pass the relative path to FindFirst or FindFirstFile .

No path component in this file name is greater than the MAX_PATH characters, so you must work every time in directories.

Remember that multi-threaded programs may be sensitive to changes in the current directory, since the process has only one current directory, common to all threads.

0
source share

All Articles