Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Folder instead Files

Status
Not open for further replies.

zero1de

Technical User
Oct 17, 2005
4
My script goes through folders and subfolders but deletes the files instead of folders.
I want to delete if i have in Folder \\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130101 6 Folder like this
\\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130102
\\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130103
\\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130104
\\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130105
\\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130106
\\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130107

Delete Folder \\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130106 and \\xxx\dfs\XML-Archiv\sss\pu_207\archiv\20130107
The Script should start at \\xxx\dfs\XML-Archiv\sss the next Subfolder pu_207 oder pu_208 etc. can't delete !!!

Who can help me adapt the script.

path = "\\xxx\DFS\XML-Archiv\sss\"
$keep = 5
$strLogFileName = "\\xxx\DFS\XML-Archiv\sss\Deleted_DWHXML.log";

function Log-Message
{
Param ([string]$logtext)
Add-content $strLogFileName -value $logtext
}

$dirs = Get-ChildItem -Path $path -Recurse | Where-Object {$_.PsIsContainer}
foreach ($dir in $dirs) {
$files = Get-ChildItem -Path $dir.FullName | Where-Object {-not $_.PsIsContainer -and $_.name -like "*.zip"}
if ($files.Count -gt $keep) {
$files | Sort-Object LastWriteTime | Select-Object -First ($files.Count - $keep) |
% { $dt=get-date;(Log-Message "Deleting File $_ on $dt");$_ }| Remove-Item -Force

}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top