Earlier I had posted my .vbs script for deleting files that were more than ‘x’ days old.
I just ran across this script that accomplishes the same thing, using Windows 2003′s native forfiles comand (scroll down to step 4).
echo on
rem First Delete old SQL Backup Files
FORFILES /p C:\filename /s /m *.* /d -3 /c “CMD /C del /Q @FILE”
rem pause
Save the script with a .bat or .cmd extension and schedule it using the Windows Scheduled Tasks applet.
December 20, 2007 at 6:29 am
Excelent – this what I wanted instead of big scary vbs script
and since I wanted to securely erase files – I just put instead of del – sdelete – nice and clean solution
January 1, 2008 at 8:06 pm
[...] just added another script that automatically removes files older than ‘x’ days which uses the forfiles command [...]
August 19, 2008 at 1:47 am
Thanks for making life easier.
December 8, 2008 at 11:54 am
Thank you for this! I’ve been trying to find a good way to remove old SQL backups and this just hit the nail on the head.
April 17, 2009 at 11:32 am
Great!
May 14, 2009 at 10:40 am
Hey Julie….just wanted to let you know that “fancy quotes” in your code need to be changed before the script works
Also, is there a way to make it report out the files that it is deleting? Mine doesn’t do that. I’m using it at the top of my ntbackup script for my external harddrive….
FORFILES /p F:\ /m *.bkf /d -16 /c “CMD /C del /Q @FILE”
for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%j%%k%%l-%%i
for /f “Tokens=1″ %%i in (‘time /t’) do set tm=-%%i
set tm=%tm::=%
set dtt=%dt%%tm%
C:\WINDOWS\system32\ntbackup.exe backup systemstate “@C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\scripts\CD-Copy-S.bks” /n “CD-Copy-S-%dtt%” /d “CD-Copy-S-%dtt%” /v:no /r:no /rs:no /hc:off /m copy /j “CD-Copy-S” /l:s /f “F:\%dtt%.bkf”
May 27, 2009 at 2:06 am
forfiles command doesn’t work on windows xp.
pls suggest
Paris
May 27, 2009 at 8:22 am
Paris,
Just grab a copy of forfiles.exe from a Windows 2003 server and copy it to your XP workstation.
-Julie
May 28, 2009 at 11:02 am
Great script with forfiles command !
And if I need to keep at least 3 files in every directory too ?
June 24, 2009 at 4:36 am
big thanks for help
February 24, 2010 at 6:47 am
tested on windows 7 and server 2003, thanks.
June 9, 2010 at 1:43 pm
In the voice of comic book guy:
BEST…
COMMAND…
EVER.