1

i have a source file to copy in a target position:

aSource := 'C:\very_very_very_long_path\myfile.exe'; // over 260 chars
aTarget := 'C:\normal_path\myfile.exe';

if not(CopyFile(PChar(aSource), PChar(aTarget), false)) then
    RaiseLastOSError;

this code raise exception with code 3 - which means ERROR_PATH_NOT_FOUND.

the target and source paths exists and if i rename the source to a less long name, it works.

How can i copy a file with long path (over MAX_PATH)?

ar099968
  • 6,963
  • 12
  • 64
  • 127

2 Answers2

8

Delphi should follow the Window convention of allowing long file names when supplied with the prefix \\?\. For example, convert "D:\very long path" to "\\?\D:\very long path".

This prefix is only applicable when using the Unicode version of the API, CopyFileW in this case. If you are using Delphi 2009 or later, then CopyFile maps to CopyFileW. If you use an earlier version then you will need to explicitly call CopyFileW, and make sure that the string that you pass is UTF-16 encoded.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
  • 1
    On top of this, you must make sure that you are using the Unicode API. So in versions earlier than 2009 more work is need to call `CopyFileW`. – David Heffernan Jun 30 '17 at 09:13
  • Good point, I'll add that in. – Alex Huszagh Jun 30 '17 at 09:14
  • [Per MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#maxpath): "*Starting in Windows 10, version 1607, `MAX_PATH` limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior.*" `CopyFileW` is one of the functions. – Remy Lebeau Jun 30 '17 at 14:37
-3

A String literal only can be 255 Chars long. You can do something like this:

aSource := 'Patht 255 Chars'+'Rest of the Path';
Handas
  • 33
  • 1
  • 8
  • 4
    It's pretty clear that this isn't the problem at hand. After all, the literal used in the question isn't over 260 characters so clearly the value used has been trimmed. – David Heffernan Jun 30 '17 at 09:23