-3

I'm hoping to get a second opinion as to what this file does. We moved from an older Windows 2003 web server and ran across this file being executed from windows task scheduler.

From my review and research, this batch file scans the directory "c:\Backup" and deletes files based on a set date. If the file is older than 15 days, it removes the file.

Is my assessment correct? I'm not looking for a line by line review, just an overview confirmation would be great.

        @echo off

    setlocal

    set srcDir="C:\Backup"
    set dirMask=*.*

    if not "%1"=="" set srcDir=%1

    if not exist "%srcDir%" echo Directory %srcDir% does not exist.&goto :EOF

    call :GETPARTS "%date%

    call :SUBTRACTDAYS 15

    set cutoffDate=%yy%/%mm%/%dd%

    pushd.

    cd /D %srcDir%

    for /f "delims=" %%a in ('dir /b /a-d %dirMask%') do call :PROCESS "%%a" %%~ta

    popd

    goto :EOF

    :PROCESS

    call :GETPARTS %2

    REM ** Remove echo from the statement below to enable the actual process
    if /i "%cutoffDate%" GTR "%yy%/%mm%/%dd%" del %1

    goto :EOF

    :SUBTRACTDAYS

    set dayCnt=%1

    if "%dayCnt%"=="" set dayCnt=1

    REM Substract your days here
    set /A dd=1%dd% - 100 - %dayCnt%
    set /A mm=1%mm% - 100

    :CHKDAY

    if /I %dd% GTR 0 goto DONESUBTRACT

    set /A mm=%mm% - 1

    if /I %mm% GTR 0 goto ADJUSTDAY

    set /A mm=12
    set /A yy=%yy% - 1

    :ADJUSTDAY

    if %mm%==1 goto SET31
    if %mm%==2 goto LEAPCHK
    if %mm%==3 goto SET31
    if %mm%==4 goto SET30
    if %mm%==5 goto SET31
    if %mm%==6 goto SET30
    if %mm%==7 goto SET31
    if %mm%==8 goto SET31
    if %mm%==9 goto SET30
    if %mm%==10 goto SET31
    if %mm%==11 goto SET30
    REM ** Month 12 falls through

    :SET31

    set /A dd=31 + %dd%

    goto CHKDAY

    :SET30

    set /A dd=30 + %dd%

    goto CHKDAY

    :LEAPCHK

    set /A tt=%yy% %% 4

    if not %tt%==0 goto SET28

    set /A tt=%yy% %% 100

    if not %tt%==0 goto SET29

    set /A tt=%yy% %% 400

    if %tt%==0 goto SET29

    :SET28

    set /A dd=28 + %dd%

    goto CHKDAY

    :SET29

    set /A dd=29 + %dd%

    goto CHKDAY

    :DONESUBTRACT

    if /I %mm% LSS 10 set mm=0%mm%
    if /I %dd% LSS 10 set dd=0%dd%

    goto :EOF

    :GETPARTS

    set dt=%~1
    set tok=1-3

    if "%dt:~0,1%" GTR "9" set tok=2-4

    set yyyy=

    for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
      for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
    )

    if not "%yyyy%"=="" set yy=%yyyy%

    if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
    if 1%mm% LSS 100 set mm=0%mm%
    if 1%dd% LSS 100 set dd=0%dd%

    goto :EOF
HPWD
  • 2,232
  • 4
  • 31
  • 61
  • what is the part you don't understand? grab a book on windows batch files, and read this advice on bat debugging http://stackoverflow.com/questions/165938/how-can-i-debug-a-bat-script – PA. Sep 19 '14 at 22:16
  • 1
    "Here's a huge code dump of a batch file. Please tell me if I've guessed what it's doing correctly" isn't a viable question here (and neither is your question title, which would be absolutely meaningless if found by a future reader in a search result). Do you have a **specific** question here? – Ken White Sep 19 '14 at 22:31
  • @KenWhite I've updated the title and made an attempt to specify the question. Does this help? – HPWD Sep 22 '14 at 22:41

1 Answers1

1

Other than the date-gymnastics to calculate date-(n days), the crux of the routine is realising that yy/mm/dd increases for a later date when compared as a string, provided a century-boundary is not crossed.

Yes - it's intended to delete files older than 15 days.

miken32
  • 42,008
  • 16
  • 111
  • 154
Magoo
  • 77,302
  • 8
  • 62
  • 84