Link to home
Start Free TrialLog in
Avatar of namerg
namergFlag for United States of America

asked on

How to delete folder and files older than 60 days

I have an archive folder and tons of subfolders.
I am just wondering how to delete the folders and files files older than 60 days. Caution: If a file is not older than 60 days do not delete folder o folders but keep searching all the way down.

I have the following code but i think only works for files.
$limit60 = (Get-Date).AddDays(-60)
$path1 = "D:\Archive\Delivery"
Get-ChildItem -Path $path1 -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit60 } | Remove-Item -Force

Open in new window

Avatar of Michael Johnson
Michael Johnson
Flag of United States of America image

Create a batch file and add it to your scheduled tasks.  Something like this:

forfiles /p "C:\File\Location" -s -m *.* /D -60 /C "cmd /c del @FILE"

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles
Avatar of namerg

ASKER

Batch is an old technology . I need it in powershell.
Avatar of namerg

ASKER

Will that script accomplish what i am asking ? I do not think it does. The code i posted does that.
SOLUTION
Avatar of Michael Johnson
Michael Johnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namerg

ASKER

Hmm, i was looking for a more intelligent script though without the need to add paramaters
Avatar of DBAduck - Ben Miller
Your script works, but it won't do folders, because in the Where it has !$_.PsIsContainer which translates to a folder.  The creation time comparison is correct. If you want folders, you would want the folders to be empty right?

You could have created the folder 60 days ago, but files could have been put in there after the 60 days.  But you know your application better than we do. So if you want to delete a folder if it is in the same criteria, you can remove !$_.PsIsContainer and it will remove everything even folders.

Be aware that if the folder has files, then you would have to use the -Recurse parameter on the REmove-Item, not just the -Force.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namerg

ASKER

My bad, but the fact that the script will be part of a schedule task, adding parameters every time i wan to execute the schedule task, i do not see it efficient. I might be wrong though.

Let me rephrase my issue. I would like to delete files and folders that are older than 60 days. But what i have sees.... folders since 2015, just folders and no files. So, why keeping empty folders with no files ? i want to delete them also.

Thanks for your help,
As a scheduled task, I wouldn't expect that you would be recreating it every time you want to run.  You would just run the already configured task.  And you could have more than one task, using the same script, but just specifying different parameters in the task (for example if you had different folders that you wanted to run the cleanup on).
Command:  powershell.exe
Arguments:  -file "c:\scripts\deleteold.ps1" -folderpath "D:\Archive\Delivery" -fileage 60 -cleanfolders -logfile c:\temp\pslog1.txt

I think you've just echoed my point.  No need to keep any folders if they're empty, regardless of the date on them.  Unless for some reason your application or process creates empty folders which need to be there.

Both my code and the script linked to by Michael Johnson will delete folders.
Avatar of namerg

ASKER

hmm , you wrote "No need to keep any folders if they're empty, regardless of the date on them"...date matters. I have to treat them as files too. If they are older than x days delete them also and of course the files below of it are older too. It is kinda confusing what i am trying to express...
Please answer these questions:

1) Look at all your folders in the tree you want this script to run against.  Are there any that are empty but have a date that is newer than the cutoff date (now - 60 days)?
2) Does your application or process create empty folders which need to be there?
Avatar of namerg

ASKER

Answers below:

1) Look at all your folders in the tree you want this script to run against.  Are there any that are empty but have a date that is newer than the cutoff date (now - 60 days)? Yes, correct.

2) Does your application or process create empty folders which need to be there? Yes, but the timestamp will be newer. So, if the script is executed in the future (more than 60 days), the empty folders will be deleted.
One more question.

1) Will there be a folder that is older than 60 days and still contain files that are NEWER than 60 days?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namerg

ASKER

@DBAduck
Will there be a folder that is older than 60 days and still contain files that are NEWER than 60 days?
Yes
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial