Created
November 2, 2016 02:01
-
-
Save 9999years/897bcbd388b13be9b2477baea4bef688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#folder denesting script for powershell | |
#also deletes empty folders | |
#you'll have to run it n times for n levels of nested folders | |
#but it outputs the number of folders denested and deleted | |
#this works for 99% of scenarios, but if you run into an edge case... sorry! | |
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" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment