Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch for for deleting files older than 20 minutes  (Read 22345 times)

0 Members and 1 Guest are viewing this topic.

petersoj

    Topic Starter


    Starter

    • Experience: Experienced
    • OS: Windows 7
    Batch for for deleting files older than 20 minutes
    « on: January 20, 2014, 09:46:45 PM »
    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.



    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Batch for for deleting files older than 20 minutes
    « Reply #1 on: January 21, 2014, 04:16:54 AM »
    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.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Batch for for deleting files older than 20 minutes
    « Reply #2 on: January 21, 2014, 06:04:54 AM »
    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 :EOF

    petersoj

      Topic Starter


      Starter

      • Experience: Experienced
      • OS: Windows 7
      Re: Batch for for deleting files older than 20 minutes
      « Reply #3 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
      ----------------------------------------------------------------

      When I add /value and change the | with ^|
      I get:  Invalid Command

      Also, would get creationdate work?

      Thanks

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Batch for for deleting files older than 20 minutes
      « Reply #4 on: January 22, 2014, 11:09:28 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.

      petersoj

        Topic Starter


        Starter

        • Experience: Experienced
        • OS: Windows 7
        Re: Batch for for deleting files older than 20 minutes
        « Reply #5 on: January 23, 2014, 09:24:42 PM »
        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.

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Batch for for deleting files older than 20 minutes
        « Reply #6 on: January 24, 2014, 12:31:07 AM »
        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"