Delete Any Zip or Txt by Modified Time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DSTR3
Posts: 3
Joined: 26 Sep 2011 13:44

Delete Any Zip or Txt by Modified Time

#1 Post by DSTR3 » 26 Sep 2011 13:50

I'm trying to delete any zip or txt file that is less than or equal to one minute old. This file can be located anywhere on my hard drive, (don't know where it is or will be). I have this code but it always tells me path not found. Any help is appreciated. Thank you.

Code: Select all

@echo off
cd "C:\"
setlocal
call :DateToMinutes %date:~-4% %date:~-10,2% %date:~-7,2% %time:~0,2% %time:~3,2% NowMins
for /f "delims=" %%a in ('dir * /a-d /b') 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 "c:\*.zip","C:\*.txt"
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

DSTR3
Posts: 3
Joined: 26 Sep 2011 13:44

Re: Delete Any Zip or Txt by Modified Time

#2 Post by DSTR3 » 26 Sep 2011 14:17

Even something simpler would be better! My ears are open!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Delete Any Zip or Txt by Modified Time

#3 Post by dbenham » 28 Sep 2011 08:00

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

DSTR3
Posts: 3
Joined: 26 Sep 2011 13:44

Re: Delete Any Zip or Txt by Modified Time

#4 Post by DSTR3 » 30 Sep 2011 14:07

Thank you. I got it working but I discovered that one minute is too long and I need to get down to seconds. Maybe start with 20 seconds. How can I bump this into seconds. Also I took your advice and for now went down to one directory, which seems good for now. Thank you.

Code: Select all

Line 1   @echo off 
Line 2   cd "C:\Users\DS\Downloads"
Line 3   setlocal
Line 4   call :DateToMinutes %date:~-4% %date:~-10,2% %date:~-7,2% %time:~0,2% %time:~3,2% NowMins


Line 5   set flag=0
Line 6   for /f "delims=" %%a in ('dir *.jpg *.zip *.txt /a-d /b') do call :CheckMins "%%a" "%%~ta"

Line 7   if %flag% EQU 1 (
Line 8   msg * "Hello!"
Line 9   )
Line 10  set flag=0 
Line 11  goto :EOF

Line 11  :CheckMins
Line 12  set File=%1
Line 13  set TimeStamp=%2
Line 14  call :DateToMinutes %timestamp:~7,4% %timestamp:~1,2% %timestamp:~4,2% %timestamp:~12,2% %timestamp:~15,2%%timestamp:~18,1% FileMins
Line 15  set /a MinsOld=%NowMins%-%FileMins%
Line 16  if %MinsOld% leq 1 del %file%
Line 17  if %MinsOld% leq 1 set flag=1
Line 18  goto :EOF

Line 19  :DateToMinutes
Line 20  setlocal
Line 21  set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
Line 22  if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
Line 23  set /a dd=100%dd%%%100,mm=100%mm%%%100
Line 24  set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
Line 25  set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
Line 26  if 1%hh% LSS 20 set hh=0%hh%
Line 27  if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
Line 28  if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
Line 29  if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
Line 30  set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
Line 31  endlocal&set %6=%j%&goto :EOF



I just added Line to cover any wrapping issues in this forum.
Thank you.

Post Reply