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

Remove Empty Folders by name

$
0
0

Hi guys!

i must remove all empty folders in my PC.

Each folder can be removed only if folder start-name is not 0 or 1 or scan.

Examples:

C:\Scan --> keep

C:\Scan\Try -->delete try and subfolders, keep scan

C:\Scan\0-Try --> keep all

C:\Try -->delete all

C:\Try\1-This\2-That -->delete only 2-that 

i made this code:

function Move-EmptyDir{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)][string]$Source,
        [Parameter(Mandatory = $true)][string]$Destination,
        [ValidateNotNullOrEmpty()][regex]$NoMatch
    )

    $EmptyFolders=@()
    $AllFolders= (Dir $Source -Force -Recurse) | Where-Object{$_.Attributes -match "Directory"}
    $ExistFolders= $AllFolders | Group-Object -Property Exists | Where-Object{$_.Name -match "True"}
    Foreach($Item in $ExistFolders.Group){
        $MeltinCount= $Item.GetFileSystemInfos().count #se 0 non ha elementi
        $SimCount= (($ExistFolders.Group | Group-Object -Property Parent) | Where-Object{$_.Name -eq $Item.Name}).Count #0=no subfolders
        if($MeltinCount -eq $SimCount){
            $EmptyFolders+= $Item
        }
    }

    $Moved= @()
    $Moving= $EmptyFolders.Length -1
    
    While($Moving -ne -1){
        $OneFolder= $EmptyFolders[$Moving]
        if($OneFolder.GetFileSystemInfos().count -gt 0){
            Write-Warning "This Folder $OneFolder is not empty"
            $Moving--
        }
        elseif($OneFolder.Name -match $NoMatch){
            Write-Warning "The Folder $OneFolder Match the Exceptions"
            $Moving--
        }
        elseif($OneFolder.Parent.Name -match $NoMatch){
            Write-Host "The Parent Folder of $OneFolder Match the Exceptions" -BackgroundColor Green
            $Moved += $OneFolder
            $Place = $OneFolder.FullName -replace "^$([regex]::Escape($Source))", $Destination
            if((Test-Path $Place) -eq $false){
                New-Item -Path $Place -ItemType Directory
                Remove-Item -Path $OneFolder.fullname -Force -ErrorAction SilentlyContinue
                Write-Verbose ('Moved folder "{0}" to "{1}"' -f $OneFolder.FullName, $Place)
                $Moving --
            }
            else{
                Remove-Item -Path $OneFolder.FullName -Force -ErrorAction SilentlyContinue
                $Moving --
            }
        }
        else{
            $Moved += $OneFolder
            $Place = $OneFolder.FullName -replace "^$([regex]::Escape($Source))", $Destination
            if((Test-Path $Place) -eq $false){
                New-Item -Path $Place -ItemType Directory
                Remove-Item -Path $OneFolder.fullname -Force -ErrorAction SilentlyContinue
                Write-Verbose ('Moved folder "{0}" to "{1}"' -f $OneFolder.FullName, $Place)
                $Moving --
            }
            else{
                Remove-Item -Path $OneFolder.fullname -Force -ErrorAction SilentlyContinue
            }
            $Moving --
        }
        return $Moved
    }
}

$Where= 'C:\temp'
$ToGo= 'C:\estinto'
$MatchList= '0','1','8','9','Scansion'
$Kind= 'Empty'
if(Test-Path $Where){
    #$TheMatch = Select-Match -FullList $MatchList
    $Folders = Move-EmptyDir -Source $Where -Destination $ToGo -NoMatch 'F' -Verbose
    $Drives = ForEach($Item in $Folders){
        Select-Object -Property @(
            @{ Name = 'Name'; Expression = { $Item.Name } }
            @{ Name = 'Percorso'; Expression = { $Item.Parent.FullName } }
        )
    }
    #Get-Report $Drives $Kind
}
else{
    Write-Error 'Invalid Path' -Category ResourceUnavailable
}

with powershell v4 can you do somthing better?

Thanks

A


Viewing all articles
Browse latest Browse all 15028

Trending Articles



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