logo

한국어
:: ##################################################################
:: #
:: # MicroFocus Fortify SCA 분석 배치 순차실행 스크립트
:: #
:: # 이 스크립트는 PROJECT_BAT에 정의된 배치파일을 순차적으로
:: # 실행하며 모든 배치파일의 실행이 완료될 경우 다시 처음부터
:: # 무한 루핑하여 실행한다.
:: #
:: # 또한 Sleeping time을 정의하여 업무시간에는 구동이 되지
:: # 않도록 설정할 수 있다.
:: #
:: # 작성자: 이존석 (hasu0707@gmail.com)
:: #
:: ##################################################################
@cls
@echo off

:: ##################################################################
:: # Project ID에 대응하는 배치파일 배열 (배열의 첨자로 구분한다)
:: ##################################################################
set "PROJECT_BAT[0]=D:\desktop\batch\test1.bat"
set "PROJECT_BAT[1]=D:\desktop\batch\test2.bat"
set "PROJECT_BAT[2]=D:\desktop\batch\test3.bat"

setlocal enableextensions enabledelayedexpansion
:: ##################################################################
:: #
:: # 기본 변수 셋팅
:: #
:: # START_INDEX:       몇번 배치부터 실행할 것인가 ?
:: # TIME_CHECK:        1이면 SLEEP시간 체크 0이면 무조건 진행
:: # SLEEP_START_TIME:  SLEEP 시작 시간 (00~23시:00~59분:00~59초)
:: # SLEEP_END_TIME:    SLEEP 마감 시간 (00~23시:00~59분:00~59초)
:: # INTERVAL_SEC:      배치 하나 실행 후 잠시 쉬는 시간 (초)
:: # LOG_FILE:          로그 파일
:: #
:: ##################################################################
set /A  "START_INDEX=0"
set /A  "TIME_CHECK=1"
set     "SLEEP_START_TIME=09:00:00"
set     "SLEEP_END_TIME=18:00:00"
set /A  "INTERVAL_SEC=30"
set     "LOG_FILE=test.log"
set /A  "x=%START_INDEX%"

:: ##################################################################
:: # 전체 배치파일의 갯수를 센다.
:: ##################################################################
set /A "i=0"
:Label_Index_Count
if defined PROJECT_BAT[%i%] (
  set /A "i+=1"
  goto :Label_Index_Count
)
set /A INDEX_COUNT=%i%

:: ##################################################################
:: # 무한루프 시작
:: ##################################################################
:Infinite_Loop
    set /A "RUN_BATCH=1"
    if %x% geq %INDEX_COUNT% (
        SET /A "x=0"
    )

:: ##################################################################
:: # 현재 시간을 구한다.
:: ##################################################################
    call :getTime TIME_NOW

:: ##################################################################
:: # 현재 요일을 1~7의 숫자로 구한다.
:: ##################################################################
    for /F "skip=2 tokens=2-4 delims=," %%A in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set DAYNUMBER=%%A

:: ##################################################################
:: # 배치를 실행 할지 말지 결정한다.
:: ##################################################################
    if %TIME_CHECK% == 1 (
        if %DAYNUMBER% geq 6 (
            goto Label_Main
        )
        if "%TIME_NOW%" geq "%SLEEP_START_TIME%" (
            if "%TIME_NOW%" lss "%SLEEP_END_TIME%" (
                set /A "RUN_BATCH=0"
                echo [%x%] Sleeping time: %SLEEP_START_TIME% ~ %SLEEP_END_TIME%
            )
        )
    )

:Label_Main
    if %RUN_BATCH% == 1 (
        echo.
        echo #################################################
        echo TIME_CHECK:%TIME_CHECK% / INDEX: %x%
        echo Batch file: !PROJECT_BAT[%x%]!
        echo Current Time: %TIME_NOW%,  Day of Week: %DAYNUMBER%
        echo #################################################
        call %%PROJECT_BAT[%x%]%%
        echo %DATE% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2% !PROJECT_BAT[%x%]! 1>> %LOG_FILE%
    )
:Label_Next
    SET /A "x+=1"

:: ##################################################################
:: # Run interval
:: ##################################################################
    ping 127.0.0.1 -n %INTERVAL_SEC% > nul

:: ##################################################################
:: # 무한루프 끝
:: ##################################################################
goto :Infinite_Loop

endlocal
exit /b

:: ##################################################################
:: #
:: # getTime
:: #
:: # 이 함수는 hh:mm:ss의 24시간 포맷으로 현재 시간을 반환한다.
:: # ex) call :getTime now : %now% 변수로 현재 시간이 반환된다.
:: #
:: ##################################################################
:getTime returnVar [time]
    setlocal enableextensions disabledelayedexpansion

    :: Retrieve parameters if present. Else take current time
    if "%~2"=="" ( set "t=%time%" ) else ( set "t=%~2" )

    :: Test if time contains "correct" (usual) data. Else try something else
    echo(%t%|findstr /i /r /x /c:"[0-9:,.apm -]*" >nul || (
        set "t="
        for /f "tokens=2" %%a in ('2^>nul robocopy "|" . /njh') do (
            if not defined t set "t=%%a,00"
        )
        rem If we do not have a valid time string, leave
        if not defined t exit /b
    )

    :: Check if 24h time adjust is needed
    if not "%t:pm=%"=="%t%" (set "p=12" ) else (set "p=0")

    :: Separate the elements of the time string
    for /f "tokens=1-5 delims=:.,-PpAaMm " %%a in ("%t%") do (
        set "h=%%a" & set "m=00%%b" & set "s=00%%c" & set "c=00%%d"
    )

    :: Adjust the hour part of the time string
    set /a "h=100%h%+%p%"

    :: Clean up and return the new time string
    :: endlocal & if not "%~1"=="" set "%~1=%h:~-2%:%m:~-2%:%s:~-2%,%c:~-2%" & exit /b
    endlocal & if not "%~1"=="" set "%~1=%h:~-2%:%m:~-2%:%s:~-2%" & exit /b