FileCopy fails with long paths

Post bugreports for the Windows version here
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

FileCopy fails with long paths

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: FileCopy fails with long paths

Post by jacdelad »

If I remember right windows has several problems with patnames >260 characters. That's why API calls don't work with them.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Fred
Administrator
Administrator
Posts: 18199
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: FileCopy fails with long paths

Post 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.
Fred
Administrator
Administrator
Posts: 18199
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: FileCopy fails with long paths

Post by Fred »

I tried to create a long filename on Windows 10 and Windows 11 without success. How did you create path > 260 ?
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: FileCopy fails with long paths

Post by BarryG »

It's been 2 years so I've forgotten what led to this problem now. :(
Little John
Addict
Addict
Posts: 4786
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FileCopy fails with long paths

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: FileCopy fails with long paths

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: FileCopy fails with long paths

Post 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

Post Reply