The specified path is too long
Перейти к содержимому

The specified path is too long

  • автор:

Windows: The File Name or Destination Path You Specified is Not Valid or Too Long

If your application or end-users require long file names or long path names, we strongly recommend that you include the replication option when you request or update your share(s). We have found that file names or path names beyond the 255 character limit are often impossible to restore from the DR copy.

Microsoft Windows has a MAX_PATH limit of ~256 characters. If the length of the path and filename combined exceed ~256 characters you will be able to see the path/files via Windows/File Explorer, but may not be able to delete/move/rename these paths/files.

Some Windows utilities including most versions of File Explorer will prepend the file path with “\\?” to show that the path is beyond the character limit. This display is called an extended-length path prefix.

Applications which utilize UNC paths (such as robocopy) and Macintosh (MacOS) systems accessing CIFS do not have the MAX_PATH limitation and can cause this condition.

A CIFS share containing a file structure which exceeds MAX_PATH may sometimes be remediated in the following 3 ways:

  1. Rename or move a deep folder. If possible rename or move the folder(s) so that the target files that are deeper than the MAX_PATH no longer exist. If you do this, start at the root folder (or any other convenient place), and then rename or move folders/files so that they have shorter names.
  2. Utilize UNC pathing in Windows Explorer.
    Note: This allows you to browse down the sub-folders to a path length of ~512 characters below the Network Mapped Drive. If your path length is still excessive, start this process by establishing a Network Mapped Drive at a deeper level, closer to the 256 character MAX_PATH as the starting point.
    • Open Windows/File Explorer.
    • Select the existing Network Mapped Drive to your share.
    • Using the Network Mapped Drive letter, access the UNC path.
      Enter \\?\’drive-letter’\ and then press Enter.
    • Access and correct the file system as necessary.
  3. Attempt remediation utilizing a Macintosh (MacOS) system. Since Macintosh systems aren’t limited by the Microsoft MAX_PATH limit you may be able to browse/rename the path/files as required from a MacOS system.
  4. If none of these options work for you, use the form to contact Shared File Services for guidance.

Note: The MAX_PATH limit also impacts restoration of deleted files from snapshots. If file restoration from snapshots is necessary on files which exceed MAX_PATH, it may be possible via the above methods.

Comments?

To share feedback about this page or request support, log in with your NetID

How to Fix the “Path Too Long” Error on Windows

Do you get a “Path Too Long” error while extracting a ZIP archive on your Microsoft Windows 10 or Windows 11 PC? You may have exceeded the maximum character limit allowed for your folders and files. We’ll show you how to fix that as well as other underlying items to resolve your problem.

Some reasons Windows displays the above error are that your ZIP’s file name is too long, your parent folder has a lengthy name, your ZIP itself has a problem, and File Explorer is acting up.

Table of Contents

How to Fix the “Path Too Long” Error on Windows image 1

Rename Your ZIP File

The easiest way to fix Windows’ “Path Too Long” error is by renaming your ZIP file. If your ZIP file has a lengthy name, shortening the name will allow you to bypass the above error.

Renaming your file doesn’t change the contents within.

  1. Open a File Explorer window and find your ZIP file.
  2. Right-click your file and choose Rename . Alternatively, single-click your file and press F2 on your keyboard.

How to Fix the “Path Too Long” Error on Windows image 2

  1. Enter a new short name for your file and press Enter .
  2. Right-click your ZIP file and choose Extract All .

How to Fix the “Path Too Long” Error on Windows image 3

  1. Select Extract on the following window.

Rename Your Parent Folder to Shorten the File Path

If your ZIP file’s parent folder has a lengthy name, that may be why you’re getting a “Path Too Long” error. In this case, shorten your folder’s name, and your issue will be resolved.

The reason this method works is that shortening your folder name ensures you’re well within Windows’ allowed character limit for your folders and files.

  1. Locate your ZIP’s parent folder in a File Explorer window.
  2. Right-click your folder and choose Rename . Alternatively, select the folder and press F2 .

How to Fix the “Path Too Long” Error on Windows image 4

  1. Type a new short name for your folder and press Enter .
  2. Open the folder, right-click your ZIP file, choose Extract All , and select Extract .

Extract Your ZIP in Your Drive’s Root Folder

Another way to ensure you’re well within the allowed character limit for directories is by extracting your ZIP’s contents in your drive’s root folder. This way, you won’t have any subdirectories, preventing Windows from displaying the “Path Too Long” error.

  1. Open the folder that has your ZIP file, right-click the file, and choose Copy .
  2. Open your drive’s root folder.
  3. Right-click anywhere blank in the folder and choose Paste .

How to Fix the “Path Too Long” Error on Windows image 5

  1. Extract your ZIP’s contents by right-clicking the archive, selecting Extract All , and choosing Extract .

Turn On Long Paths on Windows

Windows allows you to have up to 260 characters in your paths by default. The system will display a “Path Too Long” if you exceed this limit. Luckily, you can increase this limit on your system by toggling on an option in Registry Editor or Local Group Policy Editor.

You should enable long paths on your Windows PC if you often work with files that exceed the default limit.

Using Registry Editor

If you’re a Windows Home edition user, use this Registry Editor method to enable long paths on your PC. Windows Pro edition users can also use this method, but the Local Group Policy Editor method below will be easier for you.

  1. Open the Run dialog box using Windows + R .
  2. Type the following in the box and press Enter :
    regedit
  3. Select Yes in the User Account Control prompt.
  4. Navigate to the following path in Registry Editor:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
  5. Open the LongPathsEnabled entry located on the right.

How to Fix the “Path Too Long” Error on Windows image 6

  1. Enter 1 in the Value data field and select OK .

How to Fix the “Path Too Long” Error on Windows image 7

  1. Close Registry Editor and reboot your PC.
  2. Extract your ZIP file.
Using Local Group Policy Editor
  1. Open the Run box using Windows + R .
  2. Type the following in the box and press Enter :
    gpedit.msc
  3. Navigate to Computer Configuration > Administrative Templates > System > Filesystem using the left sidebar.
  4. Open the Enable Win32 long paths entry given on the right.

How to Fix the “Path Too Long” Error on Windows image 8

  1. Select Enabled on the open window and choose Apply followed by OK at the bottom.

How to Fix the “Path Too Long” Error on Windows image 9

  1. Close Local Group Policy Editor and restart your PC.
  2. Unarchive your ZIP file.

Extract Your ZIP Using Command Prompt or PowerShell

If you still get a “Path Too Long” error, try using Command Prompt or PowerShell to extract your archive’s contents. These utilities unarchive your archives the same way File Explorer does.

Using Command Prompt
  1. Open Start , find Command Prompt , and launch the tool.
  2. Type the following command in CMD and press Enter . Here, replace PATH with the path where your ZIP file is located.
    cd PATH
  3. Then, run the following command replacing FileName.zip with the name of your ZIP file.
    tar -xf FileName.zip

How to Fix the “Path Too Long” Error on Windows image 10

The command will extract your files in the same folder as the ZIP file.

Using PowerShell
  1. Open Start , find PowerShell , and launch the utility.
  2. Type the following command in PowerShell and press Enter . Here, replace SourcePath with the path to your ZIP file and TargetPath with the path where you want to extract your archive.
    Expand-Archive -LiteralPath “SourcePath” -DestinationPath “TargetPath”

How to Fix the “Path Too Long” Error on Windows image 11

Fix Windows File Explorer to Resolve Long Error

If you still can’t extract your ZIP archive and get a “Path Too Long” error, your File Explorer utility may be faulty. In this case, you can use your system’s built-in SFC (System File Checker) tool to try to fix your file manager.

  1. Open Start , find Command Prompt , and select Run as administrator .
  2. Choose Yes in the User Account Control prompt.
  3. Type the following command on the CMD window and press Enter : sfc /SCANFILE=C:\Windows\explorer.exe

How to Fix the “Path Too Long” Error on Windows image 12

  1. Next up, use the following command:
    sfc /SCANFILE=C:\Windows\SysWow64\explorer.exe
  2. Try to extract your archive.

Troubleshooting the Path Error on Windows PCs

There are multiple ways to get around Windows’ “Path Too Long” error and extract your archives successfully. You can try to keep your file names’ under the allowed limit, or you can expand the default path limit to bypass the error.

Either way, your issue will be fixed, and you’ll be able to extract your files without any problems.

Mahesh has been obsessed with technology since he got his first gadget a decade or so ago. Over the last few years, he’s written a number of tech articles on various online publications including but not limited to MakeTechEasier and Android AppStorm. Read Mahesh’s Full Bio

How can I handle The specified path, file name, or both are too long exception ?

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.

Should I just use try and catch and move that files to my already exist restricted files List ?
Or I should find some workaround and solve it ?

This class is where I’m doing the searching inside files contents :

using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Search < class SearchingCore < private static string restrictedFile = ""; private static ListrestrictedFiles = new List(); private static int numberofrestrictedFiles = 0; private static int numberoffiles = 0; public static IEnumerable SearchAccessibleFilesNoDistinct(string root, List files, BackgroundWorker worker, DoWorkEventArgs e, ManualResetEvent _isbackgroundbusy, TextBox textBoxSetFileExtensions, Label lblretrievedfiles, Label lblrestrictedfiles) < if(Form1.newOperation == true) < numberoffiles = 0; lblretrievedfiles.Text = "0"; Form1.newOperation = false; >_isbackgroundbusy.WaitOne(); if (files == null) files = new List(); try < if (Directory.Exists(root)) < try < foreach (var file in Directory.EnumerateFiles(root)) < _isbackgroundbusy.WaitOne(); try < if (worker.CancellationPending == true) < e.Cancel = true; return files; >restrictedFile = file; string ext = Path.GetExtension(file); if (!files.Contains(file) && ext == textBoxSetFileExtensions.Text) < files.Add(file); >numberoffiles++; lblretrievedfiles.Invoke((MethodInvoker)delegate < lblretrievedfiles.Text = numberoffiles.ToString(); lblretrievedfiles.Visible = true; >); > catch < string jj = ""; >> > catch (Exception err) < string gg = ""; >foreach (var subDir in Directory.EnumerateDirectories(root)) < _isbackgroundbusy.WaitOne(); if (worker.CancellationPending == true) < e.Cancel = true; return files; >try < SearchAccessibleFilesNoDistinct(subDir, files, worker, e ,_isbackgroundbusy, textBoxSetFileExtensions,lblretrievedfiles, lblrestrictedfiles); >catch (UnauthorizedAccessException) < restrictedFiles.Add(restrictedFile); numberofrestrictedFiles++; lblrestrictedfiles.Invoke((MethodInvoker)delegate < lblrestrictedfiles.Text = numberofrestrictedFiles.ToString(); lblrestrictedfiles.Visible = true; >); continue; > > > > catch(Exception err) < string rr = ""; >return files; > > > 

And this is in form1 :

int numberofdirs = 0; int numberoffiles = 0; void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e) < ListfilePathList = new List(); try < filePathList = SearchingCore.SearchAccessibleFilesNoDistinct(rootDirectory, null, worker, e, _isbackgroundbusy, textBoxSetFileExtensions, lblretrievedfiles, lblrestrictedfiles).ToList(); >catch (Exception err) < string hh = ""; >lblphases.Invoke((MethodInvoker)delegate < lblphases.Text = "Phase 2: Searching in files"; >); MyProgress myp = new MyProgress(); myp.Report4 = filePathList.Count.ToString(); foreach (string file in filePathList) < try < var tempFR = File.ReadAllText(file); var f = Path.GetFileName(file); _isbackgroundbusy.WaitOne(); if (worker.CancellationPending == true) < e.Cancel = true; return; >bool reportedFile = false; for (int i = 0; i < textToSearch.Length; i++) < _isbackgroundbusy.WaitOne(); if (tempFR.IndexOf(textToSearch[i], StringComparison.InvariantCultureIgnoreCase) >= 0) < if (!reportedFile) < numberoffiles++; myp.Report1 = file; myp.Report2 = numberoffiles.ToString(); myp.Report3 = textToSearch[i]; myp.Report5 = FindWordsWithCountRegex(tempFR, textToSearch).ToString(); backgroundWorker1.ReportProgress(0, myp); reportedFile = true; >> > numberofdirs++; lblsearchedfiles.Invoke((MethodInvoker)delegate < lblsearchedfiles.Text = string.Format("/", numberofdirs, myp.Report4); lblsearchedfiles.Visible = true; >); > catch (Exception err) < string fff = ""; >> > 

The exception is throw on both catch lines in form1 :

catch (Exception err)
catch (Exception err)

I’m just not sure what to do in both catch methods ? I can decide this files are restricted or somehow and I searched google and not sure yet how to solve the exception.

I saw some answer say to change the registry to allow more then 260 characters but I don’t want to change users registry without a permission. not that changing the registry will work at any machine at any time.

I’m a bit stuck and confused with that situation/s.

Best way to resolve file path too long exception

I created a app that downloads all document libraries in a SP Site , but at one point it giving me this error (I tried looking at google but couldn;t find anything, now if anyone knows any trick to solve this problem please respond otherwise thanks for looking at it)

System.IO.PathTooLongException: 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. at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck) at System.IO.Path.GetFullPathInternal(String path) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.File.Create(String path)

it reaches the limit for string, Code is given below,

#region Downloading Schemes private void btnDownload_Click(object sender, EventArgs e) < TreeNode currentNode = tvWebs.SelectedNode; SPObjectData objectData = (SPObjectData)currentNode.Tag; try < CreateLoggingFile(); using (SPWeb TopLevelWeb = objectData.Web) < if(TopLevelWeb != null) dwnEachWeb(TopLevelWeb, TopLevelWeb.Title, tbDirectory.Text); >> catch (Exception ex) < Trace.WriteLine(string.Format("Exception caught when tried to pass TopLevelWeb:, Title = , object data to (dwnEachWeb_method), Exception: ", ex.ToString(), objectData.Web, objectData.Title)); > finally < CloseLoggingFile(); >> private void dwnEachWeb(SPWeb TopLevelWeb, string FolderName, string CurrentDirectory) < if (TopLevelWeb != null) < if (TopLevelWeb.Webs != null) < CurrentDirectory = CurrentDirectory + "\\" + TopLevelWeb.Title; CreateFolder(CurrentDirectory); foreach (SPWeb ChildWeb in TopLevelWeb.Webs) < dwnEachWeb(ChildWeb, ChildWeb.Title, CurrentDirectory); ChildWeb.Dispose(); >dwnEachList(TopLevelWeb, CurrentDirectory); //dwnEachList(TopLevelWeb, FolderName, CurrentDirectory); > > > private void dwnEachList(SPWeb oWeb, string CurrentDirectory) < foreach (SPList oList in oWeb.Lists) < if (oList is SPDocumentLibrary && !oList.Hidden) < dwnEachFile(oList.RootFolder, CurrentDirectory); >> > private void dwnEachFile(SPFolder oFolder, string CurrentDirectory) < if (oFolder.Files.Count != 0) < CurrentDirectory = CurrentDirectory + "\\" + oFolder.Name; CreateFolder(CurrentDirectory); foreach (SPFile ofile in oFolder.Files) < if (CreateDirectoryStructure(CurrentDirectory, ofile.Url)) < var filepath = System.IO.Path.Combine(CurrentDirectory, ofile.Url); byte[] binFile = ofile.OpenBinary(); System.IO.FileStream fstream = System.IO.File.Create(filepath); fstream.Write(binFile, 0, binFile.Length); fstream.Close(); >> > > //creating directory where files will be download private bool CreateDirectoryStructure(string baseFolder, string filepath) < if (!Directory.Exists(baseFolder)) return false; var paths = filepath.Split('/'); for (var i = 0; i < paths.Length - 1; i++) < baseFolder = System.IO.Path.Combine(baseFolder, paths[i]); Directory.CreateDirectory(baseFolder); >return true; > //creating folders private bool CreateFolder(string CurrentDirectory) < if (!Directory.Exists(CurrentDirectory)) < Directory.CreateDirectory(CurrentDirectory); >return true; > //shorting string #endregion 

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *