Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find long (>255) filenames

There are some folder with more than 100 files on it. But all files and folders names broken with wrong encoding names (UTF->ANSI).

"C:\...\Госдача-Лечебни корпус\вертолетка\Госдача-Лечебни корпус\Госдача-Лечебни корпус\вертолетка\Госдача-Лечебни корпус\вертолетка\Госдача-Лечебни корпус\Госдача-Лечебни корпус\Госдача-Лечебни корпус\вертолетка\Госдача-Лечебни корпус\Госдача-Лечебни корпус\вертолетка\Госдача-Лечебни корпус\..."

Regular function Utf8ToAnsi finxing it, but FindFirst can't search folders with names longer than 255 symbols.

It gaves me only 70/100 files.

like image 262
Andrew Rumm Avatar asked Aug 11 '10 11:08

Andrew Rumm


People also ask

How do I determine file path length?

To run the Path Length Checker using the GUI, run the PathLengthCheckerGUI.exe. Once the app is open, provide the Root Directory you want to search and press the large Get Path Lengths button. The PathLengthChecker.exe is the command-line alternative to the GUI and is included in the ZIP file.


1 Answers

FindFirst wraps the Win32 API function FindFirstFile, and the Unicode version of that function can search paths up to 32,767 characters long if you prepend \\?\ to the path you're passing in, like \\?\C:\Folder\Folder\*.

Since Delphi 2009 and newer call the Unicode functions for you, you can just use FindFirst and co there. For Delphi 2007 and earlier (ANSI versions), you'll need to call FindFirstFile/FindNextFile/FindClose from Windows.pas directly. For more information check the Naming a file section of the platform SDK.

Do note that using \\?\ disables various bits of path processing though, so make sure it's a fully qualified path without any '.' or '..' entries. You can use the same trick to open file streams, rename, or copy files with longer paths.

The shell (Explorer) doesn't support this though, so you still need to limit those to at most MAX_PATH characters for things like SHFileOperation (to delete to the recycle bin) or ShellExecute. In many cases you can work around the problem by passing in the DOS 8.3 names instead of the long ones. FindFirst's TSearchRec doesn't expose the short names, but FindFirstFile's TWin32FindData structure does as cAlternateFileName.

like image 189
Zoë Peterson Avatar answered Oct 14 '22 04:10

Zoë Peterson