Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Delete Infinit Looped Folders!

$
0
0

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



Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>