1

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."

Jagan T
  • 30
  • 1
  • 5
  • i've never done that ... but i wonder if you need to re-login to trigger the change? or perhaps reboot? – Lee_Dailey Jan 03 '20 at 10:16
  • Usually changes in registry requires a restart to the explorer.exe process. You can do this from the task manager. And then from run command type "explorer.exe" – JimShapedCoding Jan 03 '20 at 10:18
  • i restarted the machine twice after the registry changes . its not worked – Jagan T Jan 03 '20 at 10:22
  • Not sure, but I think you need to use `-LiteralPath` for long file names. Unfortunately, the `New-Item` cmdlet does not have that parameter, so maybe try create the new folder locally first and then use `Copy-Item -LiteralPath ..` ? – Theo Jan 03 '20 at 10:36

1 Answers1

0

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
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • 1
    If this is a UNC path, you'll need `\\?\UNC\server\share\filename.txt` – Theo Jan 04 '20 at 20:41
  • @Theo you are right. It's specified in the [Maximum Path Length Limitation](https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#maximum-path-length-limitation) paragraph of the same document linked in my answer. However, I'm not sure whether long paths should be enabled then for server, client or both? _I won't be in want of such a knowledge, I hope:)_ – JosefZ Jan 04 '20 at 21:27
  • @Theo With this syntax you *can* access UNC paths prefixed with ```\\?\```, but then the path limitation seems to be active again. I will get path length errors on a network share with `Get-ChildItem -Recurse -LiteralPath '\\?\UNC\server\share'`. – stackprotector Aug 23 '22 at 07:31