I am writing a script that deletes backups for more than five days. I check it by directory name, not by actual date.
How to parse a directory name with a date to compare them?
Part of my script:
... foreach ($myDir in $myDirs) { $dirName=[datetime]::Parse($myDir.Name) $dirName= '{0:dd-MM-yyyy}' -f $dirName if ($dirName -le "$myDate") { remove-item $myPath\$dirName -recurse } } ...
Perhaps I am doing something wrong because it still does not delete directories last month.
The whole script with Akim's suggestions is given below:
Function RemoveOldBackup([string]$myPath) { $myDirs = Get-ChildItem $myPath if (Test-Path $myPath) { foreach ($myDir in $myDirs) {
Directory names are, for example, 09-07-2012, 08-07-2012, ..., 30-06-2012 and 29-06-2012.
source share