Script to delete all files older than 730 days from parent folder down thru subfolders except certain folders\subfolders.

Can someone help me with a  powershell script to delete files older than a certain date (like 730 days) except certain folders\subfolders with files.

Example:

C:\FTP\ - delete files
C:\FTP\Folder1\ - delete files
C:\FTP\Folder2\ - delete files
C:\FTP\Folder3\ - do NOT delete files
C:\FTP\Folder3\New\ - do NOT delete files.
C:\FTP\Folder4\ - delete files

TIA!
DBThomson76Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
What about
   C:\FTP\Folder3\ - do NOT delete files
  C:\FTP\Folder3\New\ - do NOT delete files.
  C:\FTP\Folder3\Old\ - delete files.
? Or asked another way: If a folder should not be deleted, does this apply to all of its subfolders too?
DBThomson76Author Commented:
Thanks for the response and follow up question.

In this case, I want to keep all files and all files in the subfolders of C:\FTP\Folder3 but remove all files in all other folders from root folder down that are older than 730 days.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
$exclude='C:\FTP\Folder3\','C:\FTP\FolderHasToStay\'
$age = 730   # days

$exclude = $exclRegex -replace '\\', '\\' -join '|'
$date = (get-date).AddDays(-$age)
get-childitem 'C:\FTP' -recurse | ? { !$_.PsIsContainer } |
  ? { $_.LastWriteTime -lt $date} |
  ? { $_.DirectoryName -notmatch $exclRegEx } |
  remove-item -whatif

Open in new window

This should simulate deletion of files (no folders). Remove -whatif if you are willing to let it happen.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
@Qlemo, nice script. Looks like you got your variables backward on line 4.
Here's the correct version.
$exclude='C:\FTP\Folder3\','C:\FTP\FolderHasToStay\'
$age = 730   # days

$exclRegex = $exclude -replace '\\', '\\' -join '|'
$date = (get-date).AddDays(-$age)
get-childitem 'C:\FTP' -recurse | ? { !$_.PsIsContainer } |
  ? { $_.LastWriteTime -lt $date} |
  ? { $_.DirectoryName -notmatch $exclRegEx } |
  remove-item -whatif

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Of course. Getting late here (excuses, excuses)...
DBThomson76Author Commented:
Thank you!

I will give it a try tomorrow and let you know.
DBThomson76Author Commented:
Thank you!

This worked like a charm.
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
DBThomson76, thanks but you should really choose Qlemo's post as the answer. I didn't really do anything except switch the variables on the one line.
DBThomson76Author Commented:
Can I change it now that I submitted it.

I am new to this.
DBThomson76Author Commented:
Excellent job!
DBThomson76Author Commented:
Thank you Qlemo
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.