Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

when the work look finished, the problems appear [DELETE EMPTY FOLDERS]

$
0
0

Hi!

I've created this script

$Remove=@() 
$Et1="0"
$Et2="9"

Get-ChildItem "C:\Test" -Recurse | 
Where-Object {(Get-ChildItem -Path $_.FullName) -eq $null} | 
foreach($_){ 
    $Sav=Split-Path($_) -leaf 
    if($Sav.StartsWith($Et1) -or $Sav.StartsWith($Et2)){}
    else{
        $Remove=$Remove+$_.fullname 
        Remove-Item $_.FullName -force 
    }
}

$Remove= $Remove | sort{Split-Path($_) -leaf}

$drives = foreach ($path in $Remove) {
    $prophash = @{
        Nome= split-path($path) -leaf
        Percorso = $path
    }

    #create a new object from the property hash
    New-Object PSObject -Property $prophash
    }

#embed the style in the html header
$head = @'
<Title>Deleting Empty Folder Report</Title><style>
body 
{ 
 background-color:#FFFFFF;
 font-family:Verdana;
 font-size:12pt; 
}
td, th 
{ 
 border:1px solid blue; 
 border-collapse:collapse; 
}
th 
{
 color:white;
 background-color:green; 
}
table, tr, td, th { padding: 5px; margin: 0px }
table { margin-left:50px; }</style>
'@

#create an xml document from the HTML fragment
[xml]$html = $drives | ConvertTo-Html -fragment


#check each row, skipping the TH header row
for ($i=1;$i -le $html.table.tr.count-1;$i++) {
  $class = $html.CreateAttribute("class")
}

#create the final report from the innerxml which should be html code
$body = @"<H1>Deleting Empty Folder Report for $Folder</H1>
$($html.innerxml)"@

#put it all together
$Today = Get-Date -Format "dd-MMM"
$Name = "Deleting Empty" + $Today


ConvertTo-HTML -head $head  -PostContent "<br><i>$(Get-date)</i>" -body $body | 
Out-File "C:\Log\$Name.html" -Encoding ascii

Invoke-Item "C:\Log\$Name.html"

I would expect, when I run the script, to delete only empty folders instead also deletes files...

those files aren't empty, indeed some are 2 pages doc files

Thanks

A


Viewing all articles
Browse latest Browse all 21975

Trending Articles



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