|
Answer» Back in 2006 this code was post to this forum:
@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 :EOF
This 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%
I have recently just discovered it and wanted it for a bat file I need at work. I have not been able to GET it to work because mostly the "%%~ta variable is not recognized in the program. From a command prompt it works great and pulls the date/time stamp out of the file. Can any one offer some help to try and make this code RUN in a windows 7 environment. XP if that's what it takes. THANKS in advance.
If you provide it then the link may have more info about the formats of %date% and %%~t
It looks like the PC regional settings should have the time format set to 24 hour time.This will work on any pc from XP Pro and later to delete files older than 20 minutes. At the moment it will only echo the del command to the console.
Wmic removes regional differences so it will work in any world region, but it has problems with commas in filenames and will not do anything with such files, except to print an error message.
Code: [Select]@echo off :: Wmic removes regional differences - it has problems with commas in filenames. setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%" set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
call :DateToMinutes %stamp% NowMins
for /f "delims=" %%a in ('dir * /a-d /b') do call :CheckMins "%%~fa" pause goto :EOF
:CheckMins set "filestamp=" set "filemins=" set "MinsOld=" set "YY=" & set "YYYY=" & set "MM=" & set "DD=" set "HH=" & set "Min=" & set "Sec=" & set "dt=" set "file=%~1" set "filea=%file:\=\\%" WMIC DATAFILE WHERE name="%filea%" get lastmodified | find "." >file.tmp for /f %%a in (file.tmp) do set "dt=%%a" set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%" set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" set "filestamp=%YYYY% %MM% %DD% %HH% %Min%" del file.tmp 2>nul
if not defined yyyy goto :EOF
call :DateToMinutes %filestamp% FileMins
set /a MinsOld=%NowMins%-%FileMins% :: echo Now:%NowMins% File:%FileMins% Fileage:%minsold% "%~1" if %MinsOld% gtr 1200 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 :EOFI ran the code but I am getting this for this line of code: ---------------------------------------------------------------- WMIC DATAFILE WHERE name="%filea%" get lastmodified | find "." >file.tmp
No instances available ----------------------------------------------------------------
When I add /value and change the | with ^| I get: Invalid Command
Also, would get creationdate work?
ThanksQuote from: petersoj on January 22, 2014, 10:40:04 PM I ran the code but I am getting this for this line of code: ---------------------------------------------------------------- WMIC DATAFILE WHERE name="%filea%" get lastmodified | find "." >file.tmp
No instances available ----------------------------------------------------------------
Did you try it without any changes?
Quote from: Also, would get creationdate work?
Yep. Well, the file ran correctly without changes. Good call and thanks. I was trying to manipulate the code when it didn't need it. I thought the bat file couldn't reside in the same directory as the files being evaluated/deleted for >20 min.Thanks for testing it as written, and you are right that after 20 minutes the batch file age will include itself if the filespec is * or *.*
Replace this line with the following line to fix that issue.
Code: [Select]for /f "delims=" %%a in ('dir * /a-d /b') do call :CheckMins "%%~fa" Code: [Select]for /f "delims=" %%a in ('dir * /a-d /b ^|find /i /V "%~nx0" ') do call :CheckMins "%%~fa"
|