Link to home
Start Free TrialLog in
Avatar of Ricky White
Ricky WhiteFlag for United States of America

asked on

How do I delete files from a folder by month using Powershell or CMD?

I have a folder with millions of files in it. How can I delete files from that folder month by month using powershell or CMD command?

OS = Windows

Thanks

Avatar of Robert
Robert
Flag of United States of America image

When you say by month I am assuming your referring to the modified date.
This one will delete all .txt files older than 30 days.
CMD
forfiles /p "C:\temp" /s /m *.txt /D -30 /C "cmd /c del @path"

Open in new window


Powershell (all items older than 30 days)
Get-ChildItem –Path "C:\temp" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item

Open in new window

Avatar of Ricky White

ASKER

Thanks Robert! yes, i meant File modified date.

But I would like to delete only the files that were modified in Jan 2011. And then later I would like to delete files for Feb 2011. I can run the command again as needed. Can you please suggest how that can be done?

ASKER CERTIFIED SOLUTION
Avatar of Robert
Robert
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
Thank you!