4

I have a program that downloads some files from Internet. The file name could be very long (100 chars). The user may choose to save these these files in a folder that has a very long name (200 chars). So, the total length of the path is over 260 chars.

I tried to create a function that checks if the path is too long and truncates the filename so the whole path will be exactly 260 chars. But functions in TPath class fails to work if the path is over 260 chars. More exactly GetDirectoryName raises a specific error when the path is over 260 chars, so I cannot use it to split the folder from file name.

A major design flaw in Delphi?
I think that TPath raising an error when working on long file names is a big flaw. Simply using GetDirectoryName to READ (not to write) will simply crash your program. TPath should allow processing long paths. MAYBE it should raise the error ONLY when you try to WRITE files with long path. But not even then. NTFS accepts long paths. So, why Delphi should force you to stick to 260? Some programs can handle longs paths. For example, I use Total Commander (never Explorer) so I am not affected by the long file name issue.

Any idea on how to treat this case?
Note: The download process is automated so I won't stop to ask the user to enter a new file name for each file that fails to be under 260 chars. I want to handle this silently.

Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • 1
    Prefix with \\?\ to get extended path support https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247.aspx#maxpath But then explorer won't like your files. If functions in `TPath` are not good, can you tell us which ones. – David Heffernan Jul 15 '15 at 10:01
  • GetDirectoryName raises an error when the path is over 260 chars – Gabriel Jul 15 '15 at 10:15
  • > But then explorer won't like your files - I don't want to save files with length over 260. I just want to truncate the path to exactly 260 chars. So, my files will be compatible with explorer. – Gabriel Jul 15 '15 at 10:18
  • 1
    Yes, `TPath` sucks. You can always use `ExtractFileDir` from `SysUtils`. – David Heffernan Jul 15 '15 at 10:23
  • With Delphi XE1, I just switched to the new and 'great' TPath. The thing is that TPath seems to be more up to date, to do more verifications and processing than the old SysUtils (well, in this case too many verifications). But, well... I can 'downgrade' to SysUtils for this particular case only. But again... now that I know that TPath can crash my program at ANY MOMENT, only because I access (read only) a random file/path from disk, makes me nervous. – Gabriel Jul 15 '15 at 10:29
  • @DavidHeffernan - I will accept your SysUtils solution if no other answer comes up. – Gabriel Jul 15 '15 at 10:31
  • 2
    At least in XE2 TPath was faulting with regards to network (UNC) long pathnames - I put bug report with fixed code to QC, but I think they just do not care as usual... – Arioch 'The Jul 15 '15 at 12:43
  • `TPath.GetDirectoryName()` calls `TPath.CheckPathLength(FileName, MAX_PATH)` before parsing the path (and enforces that limit on ALL platforms), but the actual parsing has no such length limitation, but its implementation is horribly complex. So those are shortcomings of `TPath`. `ExtractFilesDir()` and `ExtractFilePath()` have no such limitations, they simply truncate the path on the last `PathDelim`/`DriveDelim` character found. – Remy Lebeau Jul 15 '15 at 17:12

1 Answers1

3

Personally, my opinion is that TPath is simply wrong here. To assert that Windows paths cannot be greater than 260 characters is simply denying reality. What's more, to deny you the ability to perform text processing on paths is really quite inexplicable. In my opinion then, TPath should be avoided.

Which leads you back to the good old days. You can call ExtractFileDir from SysUtils. It works as well as it ever did.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490