637275

powershell denesting script

Oct 30th, 2016
274
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function DeNestFolders {
  2.     $ErrorActionPreference = "Stop"
  3.     echo "denesting folders!!"
  4.     $denests = 0
  5.     ls -r -directory | %{
  6.         $CurrentPath = $_.FullName
  7.         $ThisLs = ls -LiteralPath $CurrentPath
  8.         If($ThisLs.Count -gt 0) {
  9.             If(
  10.             ($ThisLs[0].GetType().Name -eq "DirectoryInfo") -and
  11.             ($ThisLs.Count -eq 1)) {
  12.                 $denests += 1
  13.                 echo "$($ThisLs[0].FullName) is nested, denesting"
  14.                 cp -LiteralPath $ThisLs[0].FullName "$CurrentPath\..\" -recurse -force
  15.                 rm -LiteralPath $ThisLs[0].FullName -recurse
  16.             }
  17.         }
  18.     }
  19.     echo "finished denesting, $denests folders denested"
  20.     $removals = 0
  21.     echo "removing empty folders!"
  22.     ls -r -directory | %{
  23.         $CurrentPath = $_.FullName
  24.         $ThisLs = ls -LiteralPath $CurrentPath
  25.         if(($ThisLs).Count -eq 0) {
  26.             $removals += 1
  27.             echo "$CurrentPath is empty, removing"
  28.             rm -LiteralPath $CurrentPath
  29.         }
  30.     }
  31.     echo "finished removing empty folders, $removals folders removed"
  32. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment