Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell command to delete subfolders without deleting the root

I'm having problems to create a PS command that allows me to delete several subfolders without deleting the roof folder.

I.E:

C:\Test has many subfolders:

  • C:\Test\Item1
  • C:\Test\Item2
  • C:\Test\Item3

And the folders Item1, Item2 and Item3 have many subfolders and files.

I would like to create a PS that would allow me to delete all the empty subfolders inside Item1, Item2 and Item3 without deleting Item1, Item2 and Item3 folders. It is possible that any of the Item folders is empty, but I don't want to delete them, just the empty content of each folder.

This is just an example, I have a have around 300 Item folders inside Test.

I usually would use this:

$path="C:\TEST"

do {
   $dir = gci $path -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName

   $dir | Foreach-Object { Remove-Item $_ }
} while ($dir.count -gt 0)

But this deletes the folder root folder (Item1, Item2 or Item3) if they are empty.

Thanks in advance.

like image 210
CrazyCow89 Avatar asked Feb 08 '17 11:02

CrazyCow89


People also ask

How do I delete files and subfolders in PowerShell?

Use the Delete() Method Every object in PowerShell has a Delete() method and you can use it to remove that object. To delete files and folders, use the Get-ChildItem command and use the Delete() method on the output. Here, this command will delete a file called “testdata. txt”.

How do I delete a subfolder without deleting files?

Use Control-A to select all the files. Now you can move them all to another folder. Clear the search box. There will only be folders left, which you can then remove (maybe checking first that there are only folders left...).

How do I delete multiple folders in PowerShell?

How To Delete A Folder And Its Subfolders With PowerShell. To delete a folder and all subfolders, run the Remove-Item command with the Recurse and Force parameters.

How do I delete all subfolders?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.


1 Answers

So you are looking to delete All Items inside Empty Subfolders or all items in General?

This will delete all Folders or Items in General inside of the Directory "C:\abc\"

$path = "C:\abc\"
Get-ChildItem -Path $path -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName }

This will delete all Folders that dont have any items in them.

$path = "C:\abc\"
Get-ChildItem -Path $path -Recurse | Where-Object {(Get-ChildItem $_.FullName).Count -eq 0} |Foreach-object {Remove-item -Recurse -path $_.FullName }

´ This will look inside "C:\abc\" Get all the children and delete all empty Directories inside the Children in your example this would be Item1,Item2,...

$Path = "C:\abc\"
$itemFolders= Get-ChildItem -Path $Path
$itemFolders| Foreach-Object {
    Get-ChildItem -Path $_.FullName |
    Where-Object {(Get-ChildItem $_.FullName).Count -eq 0} | 
    Foreach-object {Remove-item -Recurse -path $_.FullName }
}

Just a quick and dirty bit of Code as I dont have much time, hope I could be of help.

EDIT: Here is what i came up with, its not as performant as I'd like but it gets the job done and is fairly quick, try it out for yourself it worked for me - even threw in a couple of comments and output to clarify what's going on.

$Path="C:\abc\"
$itemFolders = Get-ChildItem $Path
#Get All Folders inside
$AllFolders = $itemFolders | Get-ChildItem -Recurse | Where-Object {$_.PSIsContainer} | Select -Property FullName
#First delete all files older than 30 days
$itemFolders | Get-ChildItem -Recurse -File | ForEach-Object{
    $limit = (Get-Date).AddDays(-30)
    if($_.LastWriteTime -lt $limit)
    {
        "{0} hasn't been modified in the last 30 days deleting it" -f $_.FullName
        Remove-Item -Path $_.FullName -Force -ErrorAction SilentlyContinue        
    }
}
#Check if there are files inside that are not containers
$AllFolders | ForEach-Object{
    $files = Get-ChildItem -File -Recurse -Path $_.FullName
    $directories = Get-ChildItem -Directory -Recurse -Path $_.FullName

    #If There are any files inside the folder dont delete it.
    if($files.Count -gt 0)
    {
        "Found {0} files inside {1} do not delete this" -f $files.Count, $_.FullName
    }
    #If There are no files and no directories inside delete it.
    elseif($files.Count -eq 0 -and $directories.Count -eq 0)
    {
        "Empty Folder {0} deleting it" -f $_.FullName
        Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
    }
    #If there are no files and empty directories inside delete it.
    elseif($files.Count -eq 0 -and $directories.Count -gt 0)
    {
        "No Files but directories found in {0} since its recursive delete it" -f $_.FullName
        Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue        
    }
}
like image 153
Fabian Fulde Avatar answered Nov 11 '22 13:11

Fabian Fulde