jeudi 3 janvier 2019

How to use if-statement with forfiles to match a pattern and delete if older than x day?

How to use if else statement in forfiles to match a filename pattern and keep files for 90 days deleting older files, and if pattern not matched then keep for 30 days.

The script need to check a path recursively for all files and retain file for 90 days if *pattern.log is matched and for all others retain for only 30 days. - The folders should be kept anyways; - This contains a huge number of file and using only Powershell makes it very slow and take more than a days time, so finally I'm stuck with using forfiles with if-else condition. I'm fairly new to scripting and unable to find how I can use if-else matching a pattern.

Get-ChildItem -Path C:\Data | ForEach-Object {
    $serverpath = $_.FullName
    forfiles /p "$serverpath\Logs" /s /d -30 /c "cmd /c del /Q /F @file"
}

This code works fine and delete all the files older than 30 days. However, Now I am required to keep a matching pattern file name for 90 days which I'm stuck with. Please help.

Aucun commentaire:

Enregistrer un commentaire