问题
I need to create folder with long file path more than 250 char using powershell cmd.
I did the following things,
1, In this registry path "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem", I had changed the value for LongPathsEnabled as '1'
2,I have enabled long file path in "Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem >Enable Win32 long paths" (gpedit.msc)
But while creating a folder with more than 250 char using power shell cmd , its throwing a error as below
"New-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."
回答1:
Apply the \\?\
prefix as in the following example:
$longPart = $( 65..86 | ForEach-Object {
[string][char]$_ * 10
} ) -join '\'
$Path = "\\?\D:\PShell\DataFiles\$longPart"
$longPart.Length, $Path.Length -join ','
New-Item -ItemType Directory -Path $Path
241,265 Directory: \\?\D:\PShell\DataFiles\AAAAAAAAAA\BBBBBBBBBB\CCCCCCCCCC\DDDDDDD DDD\EEEEEEEEEE\FFFFFFFFFF\GGGGGGGGGG\HHHHHHHHHH\IIIIIIIIII\JJJJJJJJJJ\KKKKK KKKKK\LLLLLLLLLL\MMMMMMMMMM\NNNNNNNNNN\OOOOOOOOOO\PPPPPPPPPP\QQQQQQQQQQ\RRR RRRRRRR\SSSSSSSSSS\TTTTTTTTTT\UUUUUUUUUU Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 03.01.2020 22:58 VVVVVVVVVV
About the \\?\ prefix:
For file I/O, the "
\\?\
" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system....
Because it turns off automatic expansion of the path string, the "
\\?\
" prefix also allows the use of "..
" and ".
" in the path names, which can be useful if you are attempting to perform operations on a file with these otherwise reserved relative path specifiers as part of the fully qualified path.
Note that you cannot use the "\\?\
" prefix with a relative path.
Works with the following LongPathsEnabled
in registry:
reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\FileSystem" -v long*
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled REG_SZ 1
来源:https://stackoverflow.com/questions/59573783/enabling-long-file-path-in-registry-and-gpedit-msc-is-not-working