如果您曾经遇到过这个问题,那么它可能对您来说是一个简单的解决方法。如果您已经两次以上看到此错误,那么您也知道有时修复它可能是一个复杂的问题。
我们希望您只遇到简单的修复类型,但我们会为您准备不太容易的,保证也可以修复。
为什么文件名长度在 Windows 中甚至是一个问题?(Why Is Filename Length Even An Issue In Windows?)
对于像Windows(Windows)这样的操作系统来说,文件名长度一直是个问题。曾经有一段时间,文件名不能超过 8 个字符加上 3 个字符的文件扩展名。你能做的最好的事情就是myresume.doc。这是文件系统设计的限制。
随着新版本Windows(Windows)的推出,情况变得更好。我们从一个旧的、有限的文件系统转变为一种称为新技术文件系统(New Technology File System)( NTFS ) 的东西。NTFS将我们带到了一个文件名可能有 255 个字符长的地步,而文件路径长度可能会达到 32,767 个字符。那么我们怎么可能有太长的文件名呢?
Windows有称为系统变量的东西。这些是 Windows 运行所依赖的变量,(Windows)因为Windows将始终知道变量的含义以及它们的位置,即使我们在各处移动位和字节也是如此。系统变量MAX_PATH是将文件名和文件路径限制在 260 个字符以下的变量。
作为一个变量,你会认为我们可以改变它。不,我们不应该。这就像从毛衣中拉出一根线。一旦一个系统变量发生变化,其他系统变量和依赖于它们的组件就会开始分解。
那我们该如何解决呢?
轻松修复(The Easy Fix)
如果你幸运的话,你会得到错误并且确切地知道是什么文件名导致了这个问题。或者至少在哪里可以找到文件。也许你有一个看起来像这样的文件名:
C:\User\guymc\Documents\My Resumesresumewithanamesolongthatitcausesproblemsandbecomespartofsomeguysarticleonthewebhowdoyoulikemenow.docx
在这种情况下,罪犯是谁很明显。在Windows Explorer(Windows Explorer)或Windows 10中调用的文件资源管理(File Explorer)器中找到该文件,单击一次,按F2重命名它,然后将那个愚蠢的文件名更改为更合理的名称。问题(Problem)解决了。
不太容易的修复(The Less Easy Fixes)
解决这个问题并不总是那么容易。有时您可能出于某种原因无法更改文件或目录的名称。
以下解决方案将为您解决问题。它们并不难做到。
(Move)使用 PowerShell(Or Copy Files Or Directories Using PowerShell)移动、删除(Delete)或复制文件或目录
有时,您在尝试移动、删除或复制文件路径字符数超过 260 个的目录时会出错。
请注意,目录和文件夹这两个词是可以互换的。我们将继续使用“目录”。以下PowerShell cmdlet 也可用于文件。
也许文件路径看起来像:
C:\Users\guymc\Documents\This\Is\Exactly\The\Precise\Directory\Path\That\I\Need\To\Have\To\Keep\My\Files\Sorted\In\A\Manner\That\Makes\Sense\To\Me\So\Lets\Pretend\This\Is\An\Actual\Filepath\That\You\Might\Also\Have\On\Your\Windows\Computer\And\Not\Over\Think\It\Document.docx
该文件路径长度为 280 个字符。所以我们不能用普通的复制粘贴方法将目录从那里复制到其他地方。我们得到目标路径太长(Destination Path Too Long)错误。
让我们假设无论出于何种原因,我们都无法重命名嵌套文件的目录。我们做什么?
打开 PowerShell(Open PowerShell)。如果您还没有使用过PowerShell,请阅读我们的文章《为家庭用户使用 PowerShell - 初学者指南》(Using PowerShell for Home Users – A Beginner’s Guide)。不过,您可以在不阅读文章的情况下执行后续步骤。
当PowerShell打开时,您将位于用户目录的根目录。继续假设C:\Users\guymc 是(Follow)您的用户目录。
名为This的目录位于Documents目录中。要进入Documents目录,我们使用DOS命令cd Documents。
您将看到提示更改为C:\Users\guymc\Documents。那挺好的。我们正在靠近目录,这将使事情变得更容易。
使用 Copy-Item 复制目录
我们要将目录This及其内容复制到ThatNewFolder中。让我们使用带有参数-Destination和-Recurse的 PowerShell cmdlet Copy-Item。
-Destination 告诉PowerShell我们希望副本在哪里。-Recurse 告诉PowerShell将里面的所有项目复制到目标。复制将原件留在原处,并在目的地制作所有新的。
Copy-Item This -Destination ThatNewFolder -Recurse
使用 Move-Item 移动目录
假设我们要将目录 This 以及其中的所有目录和文件移动到ThatNewFolder。移动不会将原件留在原处。
我们可以使用带有参数-Path和-Destination的 PowerShell cmdlet Move-Item。-Path 定义我们想要移动的项目,而-Destination告诉PowerShell我们想要它的位置。
该 cmdlet 会将 This 放入ThatNewFolder中。它还将移动 This 目录中的所有内容。Move-Item 可用于移动文件或目录,无论文件路径或文件名长度如何,它都能正常工作。
Move-Item -Path This -Destination ThatNewFolder
为确保它正常工作,请使用cd ThatNewFolder命令进入ThatNewFolder。然后使用dir命令列出ThatNewFolder中的目录。你会看到这个目录在那里。
使用 Remove-Item 删除目录
如果我们要删除此(This )目录及其中的所有内容,我们使用Remove-Item cmdlet。
Remove-Item cmdlet 具有一些内置的安全性,因此很难删除其中包含内容的目录。在我们的示例中,我们知道我们要删除所有内容,因此我们将使用参数-Recurse使其删除内部所有内容,并使用-Force使其执行此操作,而不询问我们是否确定其中的每个项目。
被警告!恢复以这种方式删除的任何内容将非常困难。您可以尝试如何恢复意外删除的文件中的方法,但不要期望太多。
Remove-Item This -Recurse -Force
您可以再次使用 dir 命令以确保它已消失。
使 Windows 10 接受长文件路径(Make Windows 10 Accept Long File Paths)
如果您知道您将反复使用长文件路径和长文件名,那么让Windows为您工作会更容易。每天使用PowerShell完成工作毫无意义。
我们有两种方法可以做到这一点。一种适用于Windows 10 家庭(Home)版用户,另一种适用于Windows 10 专业版(Pro)或企业版(Enterprise)用户。这些方法可能适用于Windows 8.1或更早版本,但我们不能保证。
使(Make)Windows 10家庭版接受长文件路径(Home Accept Long File Paths)
要让Windows 10 Home接受长文件路径,我们需要打开注册表编辑器(Registry Editor)。如果您以前没有在注册表编辑器(Registry Editor)中工作过,请小心。不小心删除或更改此处的内容可能会使Windows无法完全正常工作。
在进行任何更改之前,请务必备份您的注册表。在我们的备份和恢复 Windows 注册表终极指南中( Ultimate Guide to Backing Up and Restoring the Windows Registry)了解您需要了解的所有内容。
打开注册表编辑器(Editor)并进行备份后,导航到位置HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem并找到键LongPathsEnabled。
双击LongPathsEnabled。在值数据:(Value data:)字段中,确保数字1在那里。单击确定(OK)以提交更改。
退出注册表编辑器(Registry Editor),您现在应该可以使用疯狂的长文件路径了。
让(Make)Windows 10专业版或企业版接受长文件路径(Pro Or Enterprise Accept Long File Paths)
为了允许Windows 10 专业版(Pro)或企业版(Enterprise)使用长文件路径,我们将使用组策略编辑器(Group Policy Editor)。它是一个工具,允许我们设置有关 Windows 如何在计算机和用户级别运行的策略。
我们有几篇关于使用组策略执行诸如禁用或启用 Internet Explorer 选项( disabling or enabling Internet Explorer options)或向登录屏幕添加消息的( adding a message to the logon screen)文章。
通过转到开始(Start)菜单并输入gpedit打开组策略编辑器(Group Policy Editor)。最上面的结果应该是Edit group policy。双击它。
组策略编辑器(Group Policy Editor)打开后,导航到Computer Configuration > Administrative Templates > System > Filesystem。在那里你会看到策略Enable Win32 long paths。
双击它以编辑策略设置。将其从Disabled更改为Enabled,然后单击OK按钮提交更改。
该政策可能不会立即生效。不过,您可以强制组策略更新( force the group policy to update)。
就是这样(That’s It)
还有一些其他方法可以解决长文件名和文件路径,但我们在这里经历的是最简单、最有效的方法。
How To Fix ‘Filename Is Too Long’ Issue In Windows
If you’ve ever seen this iѕsue, it was probably a simple fix for you. If you’ve seen thіs error more than twice, then you also know that it can be a complex іssue to fix sometimeѕ.
Let’s hope you only run into the easy fix variety, but we’ll prepare you for the less easy, guaranteed to work fixes too.
Why Is Filename Length Even An Issue In Windows?
There’s a long history of filename lengths being a problem for operating systems like Windows. There was a time when you couldn’t have filenames longer than 8 characters plus a 3-character file extension. The best you could do was something like myresume.doc. This was a restriction in place by the design of the file system.
Things got better as new versions of Windows came out. We went from an old, limited, file system to something called the New Technology File System (NTFS). NTFS took us to a point where a filename could be 255 characters long, and the file path length could potentially go up to 32,767 characters. So how can we possibly have filenames that are too long?
Windows has things known as system variables. These are variables that Windows relies upon to function, because Windows will always know what the variables mean and where they are, even when we’re moving bits and bytes all over the place. The system variable MAX_PATH is the one that restricts filenames and file paths to under 260 characters.
Being a variable, you’d think we could change it. No, we should not. It would be like pulling a thread out of a sweater. As soon as one system variable changes, other system variables and components dependent on them start to unravel.
How do we fix it, then?
The Easy Fix
If you’re fortunate, you’ll get the error and know exactly what file’s name is causing the issue. Or at least where to find the file. Maybe you have a filename that looks something like:
C:\User\guymc\Documents\My Resumesresumewithanamesolongthatitcausesproblemsandbecomespartofsomeguysarticleonthewebhowdoyoulikemenow.docx
It’s obvious who the offender is in this case. Find the file in Windows Explorer, or File Explorer as it’s called in Windows 10, click once on it, hit F2 to rename it, and change that silly filename to something more reasonable. Problem solved.
The Less Easy Fixes
It isn’t always that easy to fix this problem. Sometimes you may not be able to change the names of files or directories for whatever reason.
The following solutions will do the trick for you. They aren’t hard to do.
Move, Delete, Or Copy Files Or Directories Using PowerShell
Sometimes you get an error when trying to move, delete, or copy directories where the character count for the file path is more than 260.
Note that the words directory and folder are interchangeable. We’ll use ‘directory’ going forward. The following PowerShell cmdlets can also be used on files.
Perhaps the file path looks something like:
C:\Users\guymc\Documents\This\Is\Exactly\The\Precise\Directory\Path\That\I\Need\To\Have\To\Keep\My\Files\Sorted\In\A\Manner\That\Makes\Sense\To\Me\So\Lets\Pretend\This\Is\An\Actual\Filepath\That\You\Might\Also\Have\On\Your\Windows\Computer\And\Not\Over\Think\It\Document.docx
That file path is 280 characters long. So we cannot copy the directory out of there to somewhere else with the normal copy-paste method. We get the Destination Path Too Long error.
Let’s assume that for whatever reason, we can’t rename the directories in which the file is nested. What do we do?
Open PowerShell. If you haven’t used PowerShell yet, enjoy our article Using PowerShell for Home Users – A Beginner’s Guide. You can do the next steps without reading the article, though.
When PowerShell opens, you’ll be at the root of your user directory. Follow along assuming C:\Users\guymc is your user directory.
The directory named This is inside the Documents directory. To move into the Documents directory, we use the DOS command cd Documents.
You’ll see the prompt change to C:\Users\guymc\Documents. That’s good. We’re working closer to the directories which will make things easier.
Copy Directory Using Copy-Item
We want to copy the directory This and its contents into ThatNewFolder. Let’s use the PowerShell cmdlet Copy-Item with the parameters -Destination and -Recurse.
-Destination tells PowerShell where we want the copy to be. -Recurse tells PowerShell to copy all the items inside to the destination. Copying leaves the originals where they are and makes all new ones in the destination.
Copy-Item This -Destination ThatNewFolder -Recurse
Move Directory Using Move-Item
Let’s say we want to move the directory This, and all the directories and files in it, to ThatNewFolder. Moving does not leave the original in place.
We can use the PowerShell cmdlet Move-Item with the parameters -Path and -Destination. -Path defines the item we want to move and -Destination tells PowerShell where we want it.
The cmdlet will put This inside of ThatNewFolder. It will also move everything that is inside of the This directory. Move-Item can be used to move files or directories, and it works regardless of file path or filename length.
Move-Item -Path This -Destination ThatNewFolder
To make sure it worked, use the cd ThatNewFolder command to get into ThatNewFolder. Then use the dir command to list the directories in ThatNewFolder. You’ll see the This directory is in there.
Delete Directory Using Remove-Item
If we want to delete the This directory, and everything in it, we use the Remove-Item cmdlet.
The Remove-Item cmdlet has some built-in safety that makes it difficult to delete a directory with things inside of it. In our example, we know we want to delete everything, so we’ll use the parameters -Recurse to make it delete everything inside and -Force to make it do that without asking us if we’re sure for every item inside.
Be warned! Recovering anything deleted this way would be extremely difficult. You can try the methods in How to Recover Accidentally Deleted Files, but don’t expect much.
Remove-Item This -Recurse -Force
You can use the dir command again to make sure it is gone.
Make Windows 10 Accept Long File Paths
If you know you’re going to be using long file paths and long file names repeatedly, it’s easier to make Windows work for you. No sense using PowerShell to do the work every day.
There are two ways we can do this. One is for Windows 10 Home users and the other is for Windows 10 Pro or Enterprise users. These methods may work for Windows 8.1 or earlier, but we cannot guarantee that.
Make Windows 10 Home Accept Long File Paths
To make Windows 10 Home accept long file paths, we need to open the Registry Editor. If you haven’t worked in Registry Editor before, be cautious. Accidentally deleting or changing things in here can stop Windows from working completely.
Always make a backup of your registry before making any changes. Learn everything you need to know about that in our Ultimate Guide to Backing Up and Restoring the Windows Registry.
Once you have Registry Editor opened, and your backup made, navigate to the location HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem and find the key LongPathsEnabled.
Double-click on LongPathsEnabled. In the Value data: field, make sure the number 1 is in there. Click OK to commit the change.
Exit Registry Editor and you should be able to work with crazy long file paths now.
Make Windows 10 Pro Or Enterprise Accept Long File Paths
To allow Windows 10 Pro or Enterprise to use long file paths, we’re going to use the Group Policy Editor. It’s a tool that allows us to set policies on how Windows operates at the computer and the user levels.
We’ve got several articles on using group policy to do things like disabling or enabling Internet Explorer options, or adding a message to the logon screen.
Open the Group Policy Editor by going to the Start menu and typing in gpedit. The top result should be Edit group policy. Double-click on that.
Once the Group Policy Editor opens, navigate to Computer Configuration > Administrative Templates > System > Filesystem. There you’ll see the policy Enable Win32 long paths.
Double-click on it to edit the policy setting. Change it from Disabled to Enabled, then click the OK button to commit the change.
The policy may not take effect right away. You can force the group policy to update, though.
That’s It
There are some other ways to work around long filenames and file paths, but what we’ve gone through here are the simplest, most effective methods.