Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi Guys,
I would like to write a batch file that deletes files based on their date stamps. For example, i'd like to delete files in a folder that are 2 weeks old or more. Is there a way to do this with DOS?
Thanks

In XP, no way I know of. And in DOS, not a chance.
=====================================
If at first you don't succeed, you're about average.M2

CONSTANCIO,
It can be done with batch file, depending on your batch filling skills.
Here is the trick,
Redirect your DIR command output to a text file as,
DIR /s > 2weekold.txt
This way you get the list of directory in the form of text file.
Now, use DELETE or ERASE command with IF condition to get rid of old files. You surely need to have good command over DOS batch programming. Good luck :)
Technology Bytes Plus
http://learnitfast.blogspot.com

I would like for this batch file to compare the file dates with the system time so that the batch can be run automatically at computer startup. Once the user logs in, the batch would run and delete files that are 2 weeks old based on the system time.Once i have the batch, i'd copy it to the startup folder.

Hi Constancio
I have written one for WinXP, but it depends
a lot on your date format ect.If you "echo %date%" at the command prompt and tell me what the format is ie. mm/dd/yyyy
ect. and also try the command "dir *.* /a-d-h" and give an a sample of the output.
I,ll see what I can do.

HI Dtech10,
Here is what my output is after running the date and dir command:
Output starts here
------
C:\>echo %date%
Sun 06/22/2008C:\>dir *.* /a-d-h
Volume in drive C is Laptop Hard Disk
Volume Serial Number is 68C1-C276Directory of C:\
06/25/2007 09:03 PM 0 AUTOEXEC.BAT
03/12/2008 09:49 PM 80 BIOSID.TXT
06/25/2007 09:03 PM 0 CONFIG.SYS
09/09/2007 10:43 AM 76 DVDPATH.TXT
06/25/2007 09:08 PM 1,047 install_Log.txt
05/15/2008 06:23 PM 24 LS_TRAYAPP.ini
06/20/2008 09:23 AM 5,755 lxcj.log
06/20/2008 09:23 AM 11,610 lxcjcomx.log
06/20/2008 09:25 AM 44,143 lxcjscan.log
03/12/2008 06:26 AM 672,144 lxcjUNST.csv
03/12/2008 04:52 PM 63,750 PollSt.txt
07/21/2000 10:40 AM 2,048 w2ksect.bin
06/26/2007 01:30 PM 146 YServer.txt
13 File(s) 800,823 bytes
0 Dir(s) 2,686,590,976 bytes free
-----------------
output ends
Hope this helps
thanks
Constancio

Valerie,
FORFILES does not seem to work in XP PRO SP2. When i try to run it, i get an error like the one below:
C:\>FORFILES -pC:\ -s -m*.TXT -c"CMD /C Echo @FILE is a text file"
'FORFILES' is not recognized as an internal or external command,
operable program or batch file..
Let me know if there is anything else i can do.
Thanks
Constancio

Hi
Try this file deletion by date in a batch is quite tricky due the different date and time formats.
Valerie program looks a better bet.
If want to a batch file here's my version.
The del is remmed out for safety.@echo off
setlocal enabledelayedexpansion--
rem Delete Files on Age in Days
--rem File Age in Days to Delete
set FileAge=0rem Get File list
rem Change pushd to dir required
pushd f:\
dir *.* /a-d-h | find /v "(s)" > FileList.txtrem Get Todays Day Number
call :DayNo %Date%
set Today=%DateNo%
rem Get File list and delete if over %FileAge% days old
for /f "skip=4 tokens=1-4,*" %%a in (FileList.txt) do (
call :DayNo %%a
rem Calc File Age and delete if over %FileAge% days old
set /a CalcAge=%Today%-!DateNo!
if !CalcAge! GEQ %FileAge% (
echo %%e %%a !CalcAge! days old
echo del /p "%%e" : rem Remed out for safety
echo.
)
)
popd
rem del FileList.txt when ok
rem del FileList.txt
exit /b
rem Get Date Number
: DayNo %1
for /f "tokens=1-3 delims=/- " %%a in ("%1") do (
set /a mm=%%a
set /a dd=%%b
set /a yy=%%c
)
set /a DateNo=%dd%+(%mm%*306001/10000)+(%yy%*1461/4)
exit /b

Hello, I just ran across this post looking to accomplish the same task. I used the script you created above and changed the directory and age, but get errors when it runs.
This is what I see in cmd:
F:\>Remove_files_by_date.bat
Missing operand.
Missing operand.
Missing operand.
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).I am using XP Pro, and my echo %date% matches his as well.

Nobody likes to hear 'I tokd you so' but you'll notice this has been marinating for a couple weeks.
As soon as somebody makes 'date math' work, I'll be all ears.
=====================================
If at first you don't succeed, you're about average.M2

If you parse a 2 digit date, you'll get 08 for Aug, 09 for September... DOS numeric vars treat numbers starting with 0 as "octal".
"set /a monthvar = 07" is fine because octal 7 is also decimal 7, but "set /a monthvar = 08" is not valid because there is no such thing as octal 8, 9, A, B... octal only goes from 0 to 7... The fix... "set /a monthvar = 1%mm% - 100", now you're talking! So if "mm" equals "08", monthvar will get 8...

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |