immerkind

Powershell: Dateien nach Version löschen in mehreren Ordnern

Hallo zsm

Ich habe für mein Backup ein Skript geschrieben. Dieses soll am Ende des Skriptes im Zielordner nachschauen, wie viele Dateien es in jedem einzelnen Ordner gibt.
In diesen Unterordnern soll er die Dateien Sortieren und 30 überspringen. Alle die dann übrig sind soll er endgültig löschen.

Das ganze sieht bis jetzt so aus. Jedoch komme ich nicht weiter.

function getRecursedZIP($Folder, $Filemask="*.zip") {  
    $Results = Get-ChildItem $Folder -Filter $Filemask -Recurse -File
    return $Results
} 

$ZipFiles = getRecursedZIP -folder $DestinationArch -filemask "*.zip"  
$ZipFilesGruoped = $ZipFiles | group DirectoryName
ForEach ($file in $ZipFilesGruoped) {  
    $FilestoRemove = sort LastWriteTime -Descending | select -Skip $VersionArchive  
        try {
            remove-item -Path $FilestoRemove
        }catch {
            $errCount += 1
            Write-EventLog -LogName 'Logfile Archive IIS' -Source 'Logfile Archive' -EntryType Error -EventId 6 -Message "Error with delete archivefiles"  
        }
    }

Was genau mache ich falsch oder was habe ich vergessen?

Vielen Dank für eure Hilfe

Gruss
ImmerKind
Auf Facebook teilen
Auf X (Twitter) teilen
Auf Reddit teilen
Auf Linkedin teilen

Content-ID: 308700

Url: https://administrator.de/forum/powershell-dateien-nach-version-loeschen-in-mehreren-ordnern-308700.html

Ausgedruckt am: 26.04.2025 um 20:04 Uhr

129813
129813 01.07.2016 aktualisiert um 15:03:20 Uhr
Goto Top
Hi.
You forgot to mention what should be sorted and skipped face-wink
Simply show yourself the output of the var $ZipFilesGruoped then you see how this object looks like and which properties it has!
# ....
$FilestoRemove = $file.Group | sort LastWriteTime -Descending | select -Skip $VersionArchive
if ($FilestoRemove){
    #....try catch ...
}
# ....
Regards
ImmerKind
ImmerKind 01.07.2016 aktualisiert um 15:50:35 Uhr
Goto Top
Ich habe das Problem selber lösen können. Danke trotzdem für die Hilfe face-smile

Lösung:

remove-item -Path $FilestoRemove.FullName
129813
Lösung 129813 01.07.2016 aktualisiert um 15:55:32 Uhr
Goto Top
But you also forgot $file.Group in your sorting pipeline.
You can also do
$FilesToRemove | remove-item -Force
to remove the Items, and you should pre check if the collection is empty or not, this would be a better style instead of letting it always run into the catch.
ImmerKind
ImmerKind 01.07.2016 um 15:57:28 Uhr
Goto Top
Yes you can do it also face-smile

Thanks for your help