开发者

Batch To Delete File Less Than A Minute Old

开发者 https://www.devze.com 2023-04-07 13:55 出处:网络
I\'m trying to delete .zip, .jpg and .txt files that are under a minute old.I haven\'t added the file types yet.More about that later. But this does\'t seem to delete anything at this point.Any help i

I'm trying to delete .zip, .jpg and .txt files that are under a minute old. I haven't added the file types yet. More about that later. But this does't seem to delete anything at this point. Any help is welcomed. This i开发者_Go百科s in Windows.

 @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 %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


Firstly, your script doesn't work for me because it is dependent upon the date and time format, and for me it's:

c:\>echo %date%
Tue 27/09/2011

 c:\>echo %time%
 9:52:31.68

That aside, analyse what is going on by temporarily commenting out the @echo off at the top.

I've edited your script to handle my date format and I can see two issues:

  1. The stray white space in the passing f the fifth parameter (although this might just be a cut and paste issue); and
  2. The way you're iterating through the directories. Rather than using cd "c:**" I would alter the for statement as:

    for /f "delims=" %%a in ('dir /a-d /b /s c:\') do call :CheckMins "%%a" "%%~ta"

As PA said in the comments, there are better answers at: How can I check the time stamp creation of a file in a Windows batch script?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号