none
Delete Infinit Looped Folders!

    General discussion

  • Hello, everybody,

    today I had a problem that a backup of a file server failed again and again. The problem was that a wrong installation of a SQL Developer package created its folder infinitely often in itself. The folder could not be deleted because the path was too long.

    An attempt to delete the folder by Robocopy was unsuccessful.

    One solution was to rename the subfolders to a shorter name and after about 75 renames move the 75th subfolder under the main folder and delete the 1st subfolder. And repeat this process over and over again.

    So I scripted this process in PowerShell. See bellow:

    <#
    +--------------------------------------------------+
    | DFS-Path whose subfolders are infinitely looped..|
    +--------------------------------------------------+
    #>
    
    [String]$HeadPath = ""
        
    <#
    +-----------------------------------------+
    | Folder-Name which is infinitely looped..|
    +-----------------------------------------+
    #>
    
    [String]$LoopedFolderName = ""
    
    
    <#
    +---------------------------+
    | Script Start - main part..|
    +---------------------------+
    #>
    
    [Bool]$FoldersLeft = $true
    
    while ( $FoldersLeft -eq $true ) {    
    
        [Int]$i = 0
        [String]$Path = $HeadPath
    
        while ($i -le 75) {
            if ((Get-ChildItem -Path $Path) -eq $null) {
    
                Move-Item -Path $Path -Destination $HeadPath -Force
                    Sleep -Seconds 1
    
                [String]$RemovePath= $HeadPath + "\0"
                Remove-Item -Path $RemovePath -Force -Confirm:$false -Recurse 
                    Sleep -Seconds 1
    
                Get-Childitem -Path $HeadPath | 
                        Remove-Item -Force -Confirm:$false -Recurse           
    
                $FoldersLeft = $false
                Break
            }
    
            If ($FoldersLeft -eq $true) {
                Get-Childitem -Path $Path | 
                    Where { $_.Name -match $LoopedFolderName } |
                    Rename-Item -NewName $i
    
                $Path = $Path + "\" + $i 
                $i++
            }
        }       
            If ($FoldersLeft -eq $true) { 
                Move-Item -Path $Path -Destination $HeadPath -Force
                    Sleep -Seconds 1
    
                [String]$RemovePath= $HeadPath + "\0"
                Remove-Item -Path $RemovePath -Force -Confirm:$false -Recurse 
                    Sleep -Seconds 1
    
                Get-Childitem -Path $HeadPath | 
                        Rename-Item -NewName "0"
            }
    
            If ((Get-ChildItem -Path $HeadPath) -eq $null) {
                $FoldersLeft = $false
                break
            }
    }
    Write-Host "No Folders left!"

    For improvement suggestions or feedback I would be grateful!

    Best regards,

    Henrik


    • Edited by Henrik Piecha Wednesday, November 7, 2018 3:00 PM While Loop -le 75 - after 75 renames
    Wednesday, November 7, 2018 2:51 PM

All replies

  • Open PowerShell from a Windows 2016/Windows 10 machine which has the settting "Enable Win32 long paths" enabled, and assuming the target server has PSRemoting enabled and you are an administrator:

    Invoke-Command -ComputerName ServerHostName {Remove-Item -Force -Recurse "\\?\C:\PathToLoopedFolder"}

    For older Windows it is still technically possible via PowerShell, but you'll need to program it instead using platform invoke (aka p/invoke) techniques.

    Thursday, November 8, 2018 11:58 AM