1.

Solve : Deleting files older than 15 minutes?

Answer»

I need to create a batch file that will delete temp files in a directory. I want to delete files that are older than 15 minutes or more. I can use ?? > to cover the file names but can't narrow down the date/time. This seems like a pretty simple thing but...

Any help?

Operating system is Windows XP.Well, I could write you a QBasic Program that would do that, but it is dangerous if run automatically.

What is your application? Why do you focus on 15 minutes? Will the program be run on a scheduled bases or manually. I ask because I would have to worry about stuff like "is it midnight", "has the time adjusted (daylight savings)".

If I knew you always would run manually and not near midnight or 2am, then that would be nice.

Mac
there are ways to do time CALCULATIONS like that in batch, but things would get a bit messy. you could try lang like vbscript/perl/python etc , if you know them..
I like to use Python though. a simple ONE eg
Code: [Select]import os, time
now = time.time()
fifteen = 60 * 15  #15 min convert to secs
os.chdir("temp")
for file in os.listdir(os.getcwd()):
     m = os.stat(file)[7] #get last modified time,[8] would be last creation time
     if m - now < fifteen:
            print "File found: %s" % file
This should work in a COMMAND prompt under Windows XP.  Batch files were not really designed to handle dates or math very well, but this should work:
Code: [Select]echo off
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% gtr 15 echo 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 :EOFThis is still in "debug" mode, so after you have tested it to be sure it works the way you want it to, take the echo out of the line if %MinsOld% gtr 15 echo del %file%



Discussion

No Comment Found