I am trying to delete everything except the last 10 directories, always excluding the java directory. Unfortunately, it deletes all but the last 10 contents of the java directory.
I am trying to change the solution using the following link so that my situation works correctly: Store x number of files and delete all the 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 clear 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 }
source share