0

I found out from several places in the internet that I can use \\?\ to use the long path feature of NTFS. However, if I try to create a long path in cmd.exe with mkdir \\?\%TEMP%\a\very\long\path\follows, I get a The full path of ... is too long error.

What am I missing and how can I create a long path?

phuclv
  • 30,396
  • 15
  • 136
  • 260

1 Answers1

1

\\?\ is an extended path-length prefix that when passes to the Win32 API will make it ignore the MAX_PATH limit. It's not an NTFS feature. However it seems the mkdir command parses that string before passing to the create directory function, and that's the limitation of the command, not the OS

Key: The path can consist of any valid characters up to the maximum path length.

https://ss64.com/nt/md.html

In Windows 10 you can opt-in to remove the path limit, but on Windows 7 you have only one solution: find a program that supports long path name. One of those is the built-in robocopy which supports long path by default and you can turn that off with the option /256. You can use the /E or /CREATE option to create the folder structure by copying from a dummy file/folder. For example

C:\>robocopy /create C:\dummy C:\long\path\0123456789\1123456789\223456789\3123456789\4123456789\5123456789\6123456789\7123456789\8123456789\9123456789\0123456789\1123456789\2123456789\3123456789\4123456789\5123456789\6123456789\7123456789\8123456789\9123456789\0123456789\1123456789\2123456789\3123456789\4123456789\5123456789\6123456789\7123456789\8123456789\9123456789\

ROBOCOPY :: Robust File Copy for Windows

... <skipping many lines>

Files : *.*

Options : . /COPY:DAT /CREATE /R:1000000 /W:30

After that you can check the created folders with ls -R C:\long in PowerShell

7-zip also supports long file names. You can browse the folder structure with it and press F7 to create a new folder. Same to WinRar and Far Manager. Unfortunately they don't seem to have a command line option to create folders

AFAIK Far Manager supports long paths, too. It even supports scripting so you can write a simple script to do that. Alternatively you can call CreateDirectoryW() API directly from PowerShell to create the folder, no 3rd component is needed

$Signature  = @'[DllImport("kernel32.dll", SetLastError = true,
                CharSet = CharSet.Unicode, BestFitMapping = false)]
    public static extern bool CreateDirectory([MarshalAs(UnmanagedType.LPWStr)]string path,
                                              IntPtr securityAttributes);
'@
$Kernel32 = Add-Type -MemberDefinition $Signature -Name 'Kernel32' `
                     -Namespace 'Win32' -PassThru

$pathToCreate = "long\path" + "\0123456789" * 30 $path = "\?\C:" # or up until the folder you know that already existed foreach ($p in $pathToCreate.Split('')) { $path += '' + $p $result = $Kernel32::CreateDirectory($path, [System.IntPtr]::Zero) } if ($result) { echo "Path created successfully" }

But even then you'll face problems if you want to access the folder from Windows Explorer

Related:

phuclv
  • 30,396
  • 15
  • 136
  • 260