Page 1 of 1

Long Path Support

Posted: Wed Oct 18, 2017 3:57 pm
by mikejs
As per this doc:

https://msdn.microsoft.com/en-us/librar ... s.85).aspx

It should be possible to deal with filesystem paths that are over the old 260 character limit by prepending "\\?\" to the path (or using recent Win10 builds, but assume for the moment that we're not)

E.g. This path is invalid:

Code: Select all

C:\Users\Username\Long Path Test\LongFolderName\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername
But by changing it to this, it should work:

Code: Select all

\\?\C:\Users\Username\Long Path Test\LongFolderName\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername\Anotherlongfoldername
However, although I find that such paths do work as far as ExamineDirectory() etc are concerned - I can scan such folder structures and their contents fine, and copy files out of such long paths - things go amiss when trying to use CreateDirectory() to create them.

Using CreateDirectory on "Long Path Test" and each subfolder in turn eventually results in an apparent success, but the actual folder name created is truncated, and comes out as "Anotherlon". Looking at the entire resulting path, I find it's 257 characters, which is suspiciously close to the old 260 limit. All the subsequent folders fail with the error that there already is a folder of that name.

Has anyone done something similar where CreateDirectory has been used with such long paths? Anything extra you had to do to get it to work?

(These are test folder structures to explore the problem, but the purpose of the program involves syncing data from A to B, and I don't get to choose whether there are long paths on the source. So "don't have long paths" isn't a good option.)

(If you need to create such structures for testing purposes, md "\\?\C:\whatever\folder\you\want" works.)

Re: Long Path Support

Posted: Wed Oct 18, 2017 7:11 pm
by normeus
I was going to say, if MD \\?\ works on long names then just run command prompt for that command.
It didn't work for me.
I tried "md \\?\E:\longpath\....." and I got an error "is too long".

I know I have done it with C, I will see if I can find the code.


Norm.

Re: Long Path Support

Posted: Wed Oct 18, 2017 7:50 pm
by normeus
This works on my windows 7pro.
PB 5.60 or enable unicode if you have an older version.
You have to create the path one directory at at time. ( just like createdirectory() )

fname

Code: Select all

fname$="\\?\e:\Users\Username\Long Path Test\LongFolderName\Anotherlongfoldername\..etc..\"
 Debug CreateFile_(fname$,#GENERIC_WRITE, 0,#Null, #CREATE_ALWAYS,#FILE_ATTRIBUTE_NORMAL , #Null)

[EDIT] added the relevant code.

Norm.

Re: Long Path Support

Posted: Fri Oct 20, 2017 11:24 am
by mikejs
Well, I've made some progress, and I'm starting to think this is a bug in PB's CreateDirectory().

Using CreateDirectory() results in the creation of a folder with a truncated name, if LongPath$ is over 260 chars or so:

Code: Select all

CreateDirectory(LongPath$)
But using the Windows API call directly works fine:

Code: Select all

CreateDirectory_(LongPath$, #NUL)
(In all cases, LongPath$ is using the "\\?\" prefix)

Some of the other FileSystem functions also behave strangely. E.g., FileSize(LongPath$) returns -2 (indicating a directory) if the truncated form of LongPath$ exists, but the long version doesn't, again implying that it's doing its own truncation of the path. I suspect I can get something that works by using the native APIs in each case rather than the PB versions, but that's getting a bit messy.