637275 icon

powershell denesting script

637275 | PRO | 10/30/16 09:18:38 PM UTC | 0 ⭐ | 215 👁️ | Never ⏰ | []
PowerShell |

920 B

|

None

|

0 👍

/

0 👎

function DeNestFolders {
    $ErrorActionPreference = "Stop"
    echo "denesting folders!!"
    $denests = 0
    ls -r -directory | %{
        $CurrentPath = $_.FullName
        $ThisLs = ls -LiteralPath $CurrentPath
        If($ThisLs.Count -gt 0) {
            If(
            ($ThisLs[0].GetType().Name -eq "DirectoryInfo") -and
            ($ThisLs.Count -eq 1)) {
                $denests += 1
                echo "$($ThisLs[0].FullName) is nested, denesting"
                cp -LiteralPath $ThisLs[0].FullName "$CurrentPath\..\" -recurse -force
                rm -LiteralPath $ThisLs[0].FullName -recurse
            }
        }
    }
    echo "finished denesting, $denests folders denested"
    $removals = 0
    echo "removing empty folders!"
    ls -r -directory | %{
        $CurrentPath = $_.FullName
        $ThisLs = ls -LiteralPath $CurrentPath
        if(($ThisLs).Count -eq 0) {
            $removals += 1
            echo "$CurrentPath is empty, removing"
            rm -LiteralPath $CurrentPath
        }
    }
    echo "finished removing empty folders, $removals folders removed"
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎