I am trying to delete all but the 10 most recent directories, always excluding the java directory. Unfortunately it is deleting all but the last 10 the contents of the “java” directory too.
I’ve been trying to modify the solution from the following link to get my situation to work properly:
Keep x number of files and delete all others – PART TWO
The directory structure is as follows:
dev_app_backup\java
dev_app_backup\2012-05-09_01-00-05_commnXsl (contains Xsl files)
dev_app_backup\2012-05-09_01-00-05_published (contains zip files)
dev_app_backup\various-dates-time_commonXsl
dev_app_backup\various-dates-time_published
My plan is to run a second script to clean out the java subdirectories.
#----- define folder where files are located ----#
$TargetFolder = "\\test\TestShare\dev_app_backup\*"
#----- number of directories to keep ----#
$keep = 10
#----- get zip files based on lastwrite filter ---#
$files = Get-Childitem $TargetFolder -recurse -exclude java
if ($files.Count -gt $keep)
{
$files | Sort-Object -property $_.LastWriteTime | Select-Object -First ($files.Count - $keep) | Remove-Item -Force
}
OK, so this is the script I came up with, sorry, this is my first powershell script…
The echoes are just there to help me debug, feel free to remove them.