Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Relates to an ealier post with title:
Batch File deleting files by date?HAD TO CHANGE FOLLOWING TO GET WORKING
1. In Line:
for /f "tokens=2" %%i in ('date /t') do set thedate=%%iChange tokens=2 to tokens=*
2. all references to ~3,2 to ~0,2 and all reference to ~0,2 to ~3,2. This is to use UK date format.
Earlier post:
I have share on a server in which i have files being saved to. I need to purge this folder every so often of files that are no older then 2 days. I have found a batch file to do this. How ever on the share there are a ton of subdirectories, and my batch file only deletes in the root directory. Can someone help me out? i need to delete all of the files on that share including the files in the sub directories...What i have so far...
________
@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX
set FileDir=%~2if "%FileDir%"=="" set FileDir=.
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%set /A dd=%dd% - %OLDERTHAN%
set /A mm=%mm% + 0:LOOPDATE
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31:SET31
set /A dd=31 + %dd%
goto LOOPDATE:SET30
set /A dd=30 + %dd%
goto LOOPDATE:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29:SET28
set /A dd=28 + %dd%
goto LOOPDATE:SET29
set /A dd=29 + %dd%:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
for %%i in (%FileDir%\*.*) do (set FileName=%%i call :PROCESSFILE %%~ti)set mm=
set yyyy=
set dd=
set thedate=
goto EXIT:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD X [Dir]
ECHO Where:
echo X is the number of days previous to Today.
echo Dir is the optional directory where files exist. Defaults to current directory.
ECHO.
ECHO EX: "DELOLD 5" Deletes files older than 5 days.
echo "DELOLD 120 c:\temp Deletes files from the c:\temp directory that are older
echo than 120 days.
GOTO EXIT:PROCESSFILE
set temp=%1set fyyyy=%temp:~6%
if /I %fyyyy% LSS 100 set fyyyy=20%fyyyy%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%set fmm=%temp:~0,2%
set fdd=%temp:~3,2%:: +*************************************+
:: | This is where the files are deleted |
:: | Change the ECHO command to DEL to |
:: | delete. ECHO is used for test. |
:: +*************************************+if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (echo %FileName%)
set temp=
set fyyyy=
set fmm=
set fdd=:EXIT
___________________
chrispieReport Offensive Message For Removal
Response Number 1
Name: Mechanix2Go
Date: December 05, 2005 at 23:56:41 Pacific
Homepage: Golden-Triangle
Subject: Batch File deleting files by date?
Reply:
Hi chrispie,
Likes like you've done a good job of hammering out the 'date math'. It neatly laid out and commented.Without spending a bunch of time plowing through it and tailoring it to my DATE layout...
Could it be as simple as using DEL/S ?
Or am I missing something here?
If at first you don't succeed, you're about average.
M2
Report Offensive Follow Up For Removal
Response Number 2
Name: FishMonger
Date: December 06, 2005 at 08:16:40 Pacific
Subject: Batch File deleting files by date?
Reply:
You might want to consider using a language that is better suited for this task.
My preference would be to use Perl. Perl can do in 1 line of code everything that your current batch file does, excluding the echo statements. The echo statements would be about the same, but Perl can even do that part in fewer lines. Perl can also very easily traverse a directory tree and delete only the desired files.Report Offensive Follow Up For Removal
Response Number 3
Name: chrispie
Date: December 06, 2005 at 09:31:39 Pacific
Subject: Batch File deleting files by date?
Reply:
I've tried the Del /S command and it doesn't work for some reason. Can't figure it out. I'm no programming guru by any means.
As for Pearl I've never seen it, so i'm not sure.chrispie
Report Offensive Follow Up For Removal
Response Number 4
Name: dtech10
Date: December 06, 2005 at 15:54:52 Pacific
Subject: Batch File deleting files by date?
Reply:
Hi Chrispie
Give this a try, I've changed the date variables around, I'm english, to suit your american date format.
Change the For Loop data for your directory or file extensions.
ie.
for /r i:\xtemp %%a in (*.*) do call :CheckAge %%~sa %%~ta
or
for /r i:\xtemp %%a in (*.txt) do call :CheckAge %%~sa %%~ta
Any problems or questions just post here.
=============================================
@echo off
Rem Get Todays Date
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do (
set mm=%%a
set dd=%%b
set yy=%%c
)Rem Calculate Julian Date
set /a mm=%mm%+1
if %mm% LSS 3 (
set /a mm=%mm%+12
set /a yy=%yy%-1
)
set /a jd1=%dd%+(%mm%*306001/10000)+(%yy%*1461/4)
Rem Process Files
for /r i:\xtemp %%a in (*.*) do call :CheckAge %%~sa %%~ta
exit /b
Rem Check Age Of Each File
:CheckAge %1 %2
set XDate=%2
set yy=%XDate:~6,4%
set dd=%XDate:~3,2%
if %dd% LSS 10 set mm=%XDate:~1,1%
set mm=!XDate:~0,2!
if %mm% LSS 10 set dd=%XDate:~1,1%
set /a mm=%mm%+1
if %mm% LSS 3 (
set /a mm=%mm%+12
set /a yy=%yy%-1
)
set /a jd2=%dd%+(%mm%*306001/10000)+(%yy%*1461/4)
set /a Age=%jd1%-%jd2%
echo %1 is %Age% Days Old
if %Age% GTR 2 (
echo %1 Would Be Deleted
Rem Deleted Out for Safety
rem del %1 /p
)
echo.
Report Offensive Follow Up For RemovalResponse Number 5
Name: FishMonger
Date: December 06, 2005 at 16:22:31 Pacific
Subject: Batch File deleting files by date?
Reply:
Here is a Perl script that does the same as your batch file i.e., it only works on the one directory and only prints the filenames, it does not delete them. To delete the files, all we need to do is change 'print "$_\n"' to 'unlink $_'
#!perl -wuse strict;
usage() if (! @ARGV || $ARGV[0] =~ /\D/);
my $age = $ARGV[0];
my $dir = $ARGV[1] || '.';while (<$dir/*>) { print "$_\n" if -M $_ > $age; }
sub usage {
print <<'EOF';
USAGE:
DELOLD X [Dir]
Where:
X is the number of days previous to Today.
Dir is the optional directory where files exist. Defaults to current directory.EX: "DELOLD 5" Deletes files older than 5 days.
"DELOLD 120 c:\temp" Deletes files from the c:\temp directory that are older than 120 days.
EOF
}
Here's the modified version that desends into each sub directory.#!perl -w
use strict;
use File::Find;usage() if (! @ARGV || $ARGV[0] =~ /\D/);
my $age = $ARGV[0];
my $dir = $ARGV[1] || '.';find(sub { print "$_\n" if -M $_ > $age; }, $dir);
sub usage {
print <<'EOF';
USAGE:
DELOLD X [Dir]
Where:
X is the number of days previous to Today.
Dir is the optional directory where files exist. Defaults to current directory.EX: "DELOLD 5" Deletes files older than 5 days.
"DELOLD 120 c:\temp" Deletes files from the c:\temp directory that are older than 120 days.
EOF
}
Report Offensive Follow Up For RemovalResponse Number 6
Name: FishMonger
Date: December 06, 2005 at 16:25:16 Pacific
Subject: Batch File deleting files by date?
Reply:
If you want to leave out the usage statement, it can be done as a single line from the command prompt instead of the script.
Report Offensive Follow Up For RemovalResponse Number 7
Name: FishMonger
Date: December 06, 2005 at 16:27:59 Pacific
Subject: Batch File deleting files by date?
Reply:
If you want it to print the filenames as they are deleted, we can do this:
while (<$dir/*>) { if (-M $_ > $age) { print "$_\n" and unlink $_; }

I wanted to reply to an earlier post but this site would not let me. So I just posted the whole thing again so that if anyone else has the problem as me with this script they can see how I solved it.

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

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