This concept is scary to me - I wouldn't trust blindly deleting any TXT or ZIP file on my entire drive based solely on timestamp, but here goes.
I haven't spent much time looking at your DateToMinutes function. I'm assuming it is working properly. However, I did notice that the following line appears harmless but dead:
Code: Select all
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
You are attempting to handle 2 digit years, yet you always pass in 4 characters. If your local date is producing 4 digit years then a numeric comparison will be made and the 2 digit year processing will be skipped. If your local date is producing 2 digit years then I think your parsing of the file timestamp will be entirely wrong before you ever pass it into DateToMinutes. So I assume your local date is producing 4 digit years and the line is not needed.
Since the file timestamp is only to the minute, the actual time limit will vary between 1:00 and 1:59, depending on the seconds value of Now.
Here is an untested modification to your code that I think will work. Use it at your own risk! I recommend a backup of your system prior to running, even if this code is bug free.
Code: Select all
@echo off
setlocal
call :DateToMinutes %date:~-4% %date:~-10,2% %date:~-7,2% %time:~0,2% %time:~3,2% NowMins
for /r c:\ %%a in (*.zip *.txt) do call :CheckMins "%%a" "%%~ta"
goto :EOF
:CheckMins
set File=%1
set TimeStamp=%2
call :DateToMinutes %timestamp:~7,4% %timestamp:~1,2% %timestamp:~4,2% %timestamp:~12,2% %timestamp:~15,2%%timestamp:~18,1% FileMins
set /a MinsOld=%NowMins%-%FileMins%
if %MinsOld% leq 1 del %File%
goto :EOF
:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%
goto :EOF
Depending on the amount of info on your hard drive, it could take a LONG time to scan all the files.
As written, this script may delete ZIP and TXT files that are created or modified after the script starts but before it is finished (assuming that a parallel process writes the file). This means the MinsOld will be negative. There could also be locking issues if the file is actively being written at delete time.
Dave Benham