Folder/File path too long issue

Jack Chuong 361 Reputation points
2022-10-24T08:55:40.097+00:00

Hi all,
My users (Windows 10) often create a directory tree structure that is too long , for ex : "D:\11111111\222222222222\333333333\4444444444\55555555555\66666666\77777777\88888888\99999999\10000000..."

It leads to some troubles , such as :

  • User cannot access some of the deepest levels in the directory tree, they get error "reparse point invalid"
  • Cannot delete folder , I tried rmdir /s /q "folder path"

But I get error "not empty folder"
How can I fix it ? I want to delete folder completely.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,012 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,531 Reputation points
    2022-11-01T01:23:16.63+00:00

    I see in your replies that you reference the 8.3 folder names. Here is a Powershell script that will try to delete files/folders one by one recursively using the 8.3 name. If it encounters an error on a delete it will pause. I would suggest just stopping the script at that point because you'll just get more errors when it tries to delete the parent folders.

    See if it gives you any more information about what it can't delete.

    # https://stackoverflow.com/questions/16995359/get-childitem-equivalent-of-dir-x  
      
    function Get-ShortPathName  
    {  
        Param([string] $path)  
      
        $MethodDefinition = @'  
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW", SetLastError = true)]  
    public static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int cbShortName);  
    '@  
      
        $Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru  
        $shortPath = New-Object System.Text.StringBuilder(500)  
        $retVal = $Kernel32::GetShortPathName($path, $shortPath, $shortPath.Capacity)  
        return $shortPath.ToString()  
    }  
      
    Function LookDeeper($pf){  
        if ($pf.substring(0,1) -match 'c') {  
            "I do not process the C drive."  
            return  
        }  
        if ((get-item $pf).LinkType -eq "SymbolicLink") {  
            "Its a SymbolicLink. Skipping.  $pf"  
            return  
        }  
        $sn =  Get-ShortPathName $pf                      # get 8.3 name   
      
        if ($sn -eq "") {  
            "No shortname, unable to continue processing $pf"  
            return  
        }   
      
        try {  
            $sf = Get-ChildItem $sn -Directory -force -ErrorAction Stop             # get a list of all subdirectories  
            foreach ($f in $sf){  
                LookDeeper($f.fullname)                                            # analyze the subfolder   
            }  
              
            $sf = Get-ChildItem $sn  -force -ErrorAction Stop                      # get a collection of everything     
            foreach ($f in $sf){  
                $sn =  Get-ShortPathName $f.fullname                                # isssue delete for 8.3 name   
                if ($sn -eq "") {  
                    "No shortname, skippng {0}" -f $f.fullname   
                } else {  
                    "Removing $sn "  
                    if ((get-item $sn).LinkType -eq "SymbolicLink") {  
                        "Above file is a SymbolicLink."  
                        (get-item $sn).delete()  
                    } else {  
                        remove-item $sn -force  -ErrorAction Stop       # add the -WhatIf switch to test without actually deleting anything.  
                    }   
                }    
            }  
      
      
        } catch {  
            ""  
            "Crash: {0} " -f $_.exception  
            ""  
            "Item analysis......."  
            Get-Item $sn | Format-List -Property *  
            pause  
        }   
    }  
      
      
    Set-Location 'G:\old\'  
    LookDeeper('G:\old\Operations (OPS) - HAN')             # this starts the recursive delete   
      
    

5 additional answers

Sort by: Most helpful
  1. Castorix31 82,656 Reputation points
    2022-10-24T09:21:10.143+00:00

    Have you set LongPathsEnabled (registry or GPO) ?
    (Enable Long Paths in Windows 10, Version 1607, and Later


  2. Jack Chuong 361 Reputation points
    2022-10-25T09:46:01.143+00:00

    This is result when I try robocopy
    robocopy "G:\old\test" "G:\old\Operations (OPS) - HAN" /purge

    -------------------------------------------------------------------------------  
       ROBOCOPY     ::     Robust File Copy for Windows  
    -------------------------------------------------------------------------------  
      
      Started : Tuesday, October 25, 2022 4:38:52 PM  
       Source : G:\old\test\  
         Dest : G:\old\Operations (OPS) - HAN\  
      
        Files : *.*  
      
      Options : *.* /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30  
      
    ------------------------------------------------------------------------------  
      
                               0    G:\old\test\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG\AAA TONG HOP\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG\AAA TONG HOP\AAA LUAT KTCL\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG\AAA TONG HOP\AAA LUAT KTCL\AAA TEM MOI 1.21\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG\AAA TONG HOP\AAA LUAT KTCL\AAA TEM MOI 1.21\THANH TOAN (den 1.6.21)\  
            *EXTRA Dir        -1    G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG\AAA TONG HOP\AAA LUAT KTCL\AAA TEM MOI 1.21\THANH TOAN (den 1.6.21)\304012188120\  
      
    ------------------------------------------------------------------------------  
      
                   Total    Copied   Skipped  Mismatch    FAILED    Extras  
        Dirs :         1         0         1         0         0        10  
       Files :         0         0         0         0         0         0  
       Bytes :         0         0         0         0         0         0  
       Times :   0:00:00   0:00:00                       0:00:00   0:00:00  
       Ended : Tuesday, October 25, 2022 4:38:52 PM  
    

    "test" is an empty folder
    "Operations (OPS) - HAN" still have structure with many sub folders
    I cannot access further than "304012188120" , I get error "reparse point invalid"
    I still cannot delete folder "Operations (OPS) - HAN"


  3. Hector Xiao 21 Reputation points
    2022-10-28T01:52:46.573+00:00

    Thanks,
    Jack

    This is a limitation of the Windows file system. You will need to use a third-party tool to delete the folder, such as Unlocker.

    0 comments No comments

  4. MotoX80 32,531 Reputation points
    2022-10-28T03:09:14.283+00:00

    I enabled long paths on that workstation.

    Did you reboot after adding the registry value?

    If you've enabled long paths on the workstation, then you should be able to just delete the folder. From an admin command prompt run this...

    rd /q /s "G:\old\Operations (OPS) - HAN"   
    

    If that fails, please post the entire output.

    Then I go back to your previous comment.... "I get error "reparse point invalid"

    I wonder if your user created a symbolic link or junction that points to some other folder. Run this command and see if you find any.

    dir /al /s "G:\old\Operations (OPS) - HAN"   
    

    Have you run chkdsk on the drive? If it detects errors run it with the /f switch.

    0 comments No comments