Powershell: 'The fully qualified file name must be less than 260 characters'

给你一囗甜甜゛ 提交于 2019-12-10 15:27:32

问题


I tried to use powershell command copy-item as xcopyto copy content of one disk to another one.

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

However, I encountered the following errors:

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

I got these errors enough to discourage manually search and copy files or folders with long paths. What is the best way to avoid this problem?


回答1:


As far as I know robocopy deals with this automatically (at least you would have to disable support for long paths explicitly). So you could use

robocopy h:\ g:\ /E

if you're not too adverse to a native command instead of a pure PowerShell solution.

Usually you can prepend \\?\ to a path to allow handling paths with up to 32k characters but it could well be that this might not help with .NET.




回答2:


On Codeplex Microsoft hosts an experimental long path wrapper which provides functionality to make it easier to work with paths that are longer than the current 259 character limit of the System.IO namespace.

An example of how to copy a file using this wrapper in 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)

Other samples can be found here.



来源:https://stackoverflow.com/questions/10224545/powershell-the-fully-qualified-file-name-must-be-less-than-260-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!