using FOR command to delete files X days old

November 2, 2010 at 11:06:32
Specs: Windows XP
Hi All,

I am trying to use the FOR command to purge files in a directory that are older than a certain date. Here is what I have so far:

set folder=%C:\test\

for /R %folder% %%A in (.) do If %%~tA lss 12/08/2009 del /F /Q %%A

What this does is just goes into the given directory, and deletes everything, even though i am using the ~t syntax.

is there another way to using this syntax properly, or does it just not apply here?

See More: using FOR command to delete files X days old

Report •

November 2, 2010 at 12:25:39
'IF' doesn't compare dates as far as I know. You can format the date in the form of a number that it can compare though, YYYYMMDD.

Note that in the sample code below there is an "echo." before the delete, just for safety's sake. You can remove the echo once you're satisfied it works.

@echo off


set folder=C:\test\
set DATECODE=20080701

for /R "%folder%" %%A in (*) do (
set FILEDATE=%%~tA
If !FILEDATE! lss !DATECODE! echo.del /F /Q "%%A"

Report •

November 2, 2010 at 13:07:47

This works beautifully. Thanks so much for showing me how to do this. I am relatively new at this and any help is great help.

good day to you.

Report •

Related Solutions

Ask Question