Folder/File path too long issue

Jack Chuong 411 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,715 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 34,431 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. Jack Chuong 411 Reputation points
    2022-10-31T07:29:34.757+00:00

    Hi @MotoX80
    This is output

    rd /q /s "G:\old\Operations (OPS) - HAN"  
    G:\old\Operations (OPS) - HAN\OPERAT~1\ELECTR~1\KIEMTR~1\THONGS~1\KIEMTR~1\AAATON~1\AAALUA~1\AAATEM~1.21\THANHT~1.21)\304012~1 - The directory is not empty.  
      
    dir /al /s "G:\old\Operations (OPS) - HAN"  
     Volume in drive G is DATA  
     Volume Serial Number is E847-0B02  
      
     Directory of G:\old\Operations (OPS) - HAN  
      
    24/10/2022  03:23 PM    <DIR>          Operation  
                   0 File(s)              0 bytes  
      
     Directory of G:\old\Operations (OPS) - HAN\Operation  
      
    24/10/2022  03:22 PM    <DIR>          ELECTROLUX-HN  
                   0 File(s)              0 bytes  
      
     Directory of G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN  
      
    24/10/2022  03:22 PM    <DIR>          KIEM TRA CHAT LUONG  
                   0 File(s)              0 bytes  
      
     Directory of G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG  
      
    24/10/2022  03:22 PM    <DIR>          THONG SO KT  
                   0 File(s)              0 bytes  
      
     Directory of G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT  
      
    24/10/2022  03:22 PM    <DIR>          KIEM TRA CHAT LUONG  
                   0 File(s)              0 bytes  
      
     Directory of G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG  
      
    24/10/2022  03:22 PM    <DIR>          AAA TONG HOP  
                   0 File(s)              0 bytes  
      
     Directory of G:\old\Operations (OPS) - HAN\Operation\ELECTROLUX-HN\KIEM TRA CHAT LUONG\THONG SO KT\KIEM TRA CHAT LUONG\AAA TONG HOP  
      
    24/10/2022  03:22 PM    <DIR>          AAA LUAT KTCL  
                   0 File(s)              0 bytes  
      
     Directory of 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  
      
    24/10/2022  03:22 PM    <DIR>          AAA TEM MOI 1.21  
                   0 File(s)              0 bytes  
      
     Directory of 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  
      
    24/10/2022  03:22 PM    <DIR>          THANH TOAN (den 1.6.21)  
                   0 File(s)              0 bytes  
      
     Directory of 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)  
      
    24/08/2022  06:25 PM    <DIR>          304012188120  
                   0 File(s)              0 bytes  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.