Powershell: "A valid file name must be less than 260 characters"

I tried using the powershell command copy-itemas xcopyto copy the contents of one drive to another.

copy-item -Path h:\* -Destination g:\ -Recurse -Force

However, I encountered the following errors:

Copy-element: the specified path, file name, or both are too long. the full file name must be less than 260 characters, and the directory name must be less than 248 characters.

I got these errors enough to prevent the manual search and copying of files or folders with long paths. What is the best way to avoid this problem?

+3
source share
2 answers

, robocopy ( , ). ,

robocopy h:\ g:\ /E

PowerShell.

\\?\ , 32 000 , , .NET.

+6

Codeplex Microsoft , , , , 259 System.IO.

Powershell:

[reflection.assembly]::loadfile("C:\Users\stackoverflow\Desktop\Microsoft.Experimental.IO.dll")
[microsoft.experimental.io.longpathfile]::Copy((gi .\myversion.txt).fullname, "C:\users\stackoverflow\desktop\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",$true)

.

+5

All Articles