Hello everyone !
I have the following folder structure:
-Projects -->(root folder)
-Project1 (2nd level subfolder)
-Project2 (2nd level subfolder)
-Project3 (2nd level subfolder)
-Project4 (2nd level subfolder)
- etc` ..
Goals:
1) I'd like to keep ALL 1st level folders (project1,2,3,4.....etc) no matter what.
2) Delete only the contents of the project folders (files+subfolders in all depths) ONLY if the content is older than 14 days, in no situation should the 1st level folders be deleted, just their contents based on older than 14 days.
3) The ability to exclude certain 2nd level subfolder projects/3rd level subfolders paths inside projects.
I am using a test folder to run the tests:
- C:\Drivers\Win (as root folder)
- AUDIO (2nd level subfolder)
- DISPLAY6 (2nd level subfolder)
- INTELINF (2nd level subfolder)
- MONITOR (2nd level subfolder)
trying to run the following code:
cls $ErrorActionPreference = 'Stop' $root = 'C:\Drivers\win' $ExcludePaths = '\AUDIO', '\INTELINF\infinst_autol' $date = (Get-Date).AddDays(-14) get-childitem $root | where { $_.PsIsContainer } | get-childitem -recurse | % { $found = $false foreach ($path in $ExcludePaths) {if ($found = $found -or ($_.FullName -like "$root$path\*")) {break}} if (!$found) {$_} } | where { $_.LastWriteTime -lt $date } | Remove-Item -Recurse -WhatIf
results in 2 issues:
1. $ExcludePaths only excludes the 1st mentioned exclusion and ignores the 2nd (from the example above -\AUDIO gets excluded, \INTELINF or \INTELINF\infinst_autol does not get excluded and being deleted.
2. $ExcludePaths is unable to exclude 3rd level subfolders such as: \AUDIO\Folder , only manages to exclude the 2nd level subfolder \AUDIO .
Any chance you can help out by fixing/rewriting the script ??
Thanks in advance :-)