robocopyrmdir

RMDIR does not delete nested directories path greater than 255 length


I am using below command inside the batch file and running via task scheduler in my server , it deletes most of the nested files/folders but when path length exceed 255 it does not delete those files/folders. Is this issue with RMDIR , What are other alternatives , Robocopy Delete ? Has somebody faced this issue and robocopy was able to solve this issue for them , something like below ?

RMDIR "Path-location" /S /Q -- Does not delete files/folder > 255 length

Robocopy : You need to create empty folder before executing below commands :

robocopy "C:\empty" "C:\Test Folder" /purge
rmdir "C:\Test Folder"  // Once all files/folders are deleted now remove your parent/root directory

Solution

  • This is normal behaviour. To be exact, the limit is defined as 260 characters (source). You have multiple options to circumvent this limit:

    1. You can raise this limit to 65535 characters by adding/setting the registry value HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled of type REG_DWORD to 1. This works since Windows 10, version 1607 (read more). Reboot your machine after this change.

    2. You can use robocopy in combination with the /PURGE parameter to synchronize an empty folder to the folder that contains the long file paths as you already found out. This might get complicated if you do not want to delete everything, but only certain files.

    3. You can use UNC paths to describe long paths. Change

      C:\very\long\...\path
      

      to

      \\?\C:\very\long\...\path