About Me

Tuesday, November 29, 2011

Batch file to delete files older than N days

Most of us remember times when we used MS-DOS commands, Norton Commander and staff like that.
Experience from that period of time is still usefull. From time to time i have to create bat files that does some boring things for me.
Its great to find something new in that area.
Today, thankfully to  guys from the stackoverflow, I found really easy way how to delete files older then N days.
I never know about forfiles command, its really awesome :

forfiles /P c:\DBbackup\CT\LogShipping\ /S /M *.* /D -1 /C "cmd /c del @path"

That simple command deletes (cmd /c del @path) all files (*.*) from a path (c:\DBbackup\CT\LogShipping\) that older than one day (/D -1)

In case if you want to remind a list of available DOS commands and find ones you never know, read a list of  "An A-Z Index of the Windows CMD command line"

P.S. In case if you get errors like "Invalid argument/option - '@path'" try to remove double quotes around a path if you have any.


No comments:

Post a Comment