Page 1 of 1

FileCopy fails with long paths

Posted: Sat May 21, 2022 7:06 am
by BarryG
Since PureBasic is Unicode now, we need FileCopy() to work with long paths. Currently, it still fails if the destination file path is >260 characters, which I've just discovered with my own apps.

I found this post by normeus which fixes the issue -> https://www.purebasic.fr/english/viewto ... 79#p423879

Using his tip, I'm now using the following macro hack to "fix" FileCopy() in my sources. But can FileCopy() be fixed to do this instead? Thanks!

Code: Select all

Macro FileCopy(source,dest)
  FileCopy("\\?\"+source,"\\?\"+dest)
EndMacro
And possibly related: DeleteDirectory() isn't deleting a folder path on my PC that has >260 chars:

Code: Select all

; Don't run this as-is because it will delete data on your PC!
; Debug DeleteDirectory(path_over_260_chars$,"",#PB_FileSystem_Recursive|#PB_FileSystem_Force) ; Returns 0
Prefixing the path with "\\?\" doesn't make it work.

Re: FileCopy fails with long paths

Posted: Sat May 21, 2022 5:11 pm
by jacdelad
If I remember right windows has several problems with patnames >260 characters. That's why API calls don't work with them.

Re: FileCopy fails with long paths

Posted: Tue May 24, 2022 8:32 am
by Fred
Makes sense, as it's an unicode only feature. According to https://docs.microsoft.com/en-us/window ... reatefilea it should be the correct way to add 32k long path support.

Re: FileCopy fails with long paths

Posted: Sat Mar 22, 2025 10:20 am
by Fred
I tried to create a long filename on Windows 10 and Windows 11 without success. How did you create path > 260 ?

Re: FileCopy fails with long paths

Posted: Sat Mar 22, 2025 10:32 am
by BarryG
It's been 2 years so I've forgotten what led to this problem now. :(

Re: FileCopy fails with long paths

Posted: Sat Mar 22, 2025 1:34 pm
by Little John
Fred wrote: Sat Mar 22, 2025 10:20 am I tried to create a long filename on Windows 10 and Windows 11 without success. How did you create path > 260 ?
I did not test it, but I'm pretty sure that this can be done with programs such as e.g. Total Commander.

Re: FileCopy fails with long paths

Posted: Sat Mar 22, 2025 3:59 pm
by jacdelad
Fred wrote: Sat Mar 22, 2025 10:20 am I tried to create a long filename on Windows 10 and Windows 11 without success. How did you create path > 260 ?
Files on my NAS can have extraordinary long filenames, because the NAS's OS doesn't care. Accessing them with Windows is usually not possible.

Re: FileCopy fails with long paths

Posted: Sat Mar 22, 2025 5:42 pm
by breeze4me
Fred wrote: Sat Mar 22, 2025 10:20 am I tried to create a long filename on Windows 10 and Windows 11 without success. How did you create path > 260 ?
I tested this and the long pathname was generated correctly.

Code: Select all

Dest.s = "\\?\Z:\temp\"

For i = 0 To 1000
  p.s + "test123456\"
  Debug CreateDirectory_(Dest + p, 0)
Next

Debug Len(p)

Debug CopyFile("c:\any folder\any file.txt", Dest + p + "1.txt")

; test
If ReadFile(0, Dest + p + "1.txt")
  Debug ReadString(0)
  CloseFile(0)
EndIf