Simple Idea Windows Batch Script Library.

너무 오래되었고 MS에서도 이제는 파워쉘을 권장하는듯 보이는데 아직도 배치 스크립트를 사용하는 이유중 하나 실행이 빠르다 입니다.

큰 문제중 하나는 이 배치 스크립트가 많이 불편합니다.  특히 공통적으로 들어가는 코드부분에 있어 항상 불편하던 차에 만들기 보다는 찾아보려고 노력하였고 몇가지 를 결국 찾았지만 좀 불편하고 원하는 대로 작동이 안되었습니다.

그리하여 만들었던 코드입니다.

Test 는 해당 스크립트 파일명, lib 는 공통적으로 불러올 코드들 입니다.
스크립트 수행 도중 정상적으로 끝나지 않고 종료된다면 실행 시간이 기록된 스크립트를 남겨 버그를 찾기 편하게 만들었습니다.

it's too old and Windows already have powerful tool powershell. Why still use batch script? because Of course! batch script is so fast!

But One of Biggest Problem is too old. it's means missing so many function. Always uncomfortable especially common code part. I tried multiple code from online by others But it was not exactly what I wanted.

So I wrote this code.
call :Import "Test" "lib" %* Test is Self script name. lib is common library script name
If script exit with error during the execution of the script, This code have left the script with the execution time recorded so that you can find the bug easily.

Example.cmd:
::================================================================================
::##
::## this code written by http://project-ap.blogspot.com/ #
::##
::================================================================================
::$@test.cmd #0.001
@echo off
::$Import
call :Import "Test" "lib" %*

::$Code

goto :EOF
::$Define
if not defined Loaded_%TempCacheName% (
    :Import
    set "TempCacheName=%~1"
    for /f "tokens=2 delims==." %%a in ('wmic OS Get localdatetime /value') do set _rand=%%a"
    if not defined Loaded_%TempCacheName% (
        for %%a in (%~2) do (
            if not exist "%%a.cmd" (
                echo %%a File Not Found.
                pause
                exit
            )
        )
        set Loaded_%TempCacheName%=y
        type %~f0>_Cache_%TempCacheName%_%_rand%.cmd
        for %%a in (%~2) do (type %%a.cmd>>_Cache_%TempCacheName%_%_rand%.cmd)
        _Cache_%TempCacheName%_%_rand%.cmd %3 %4 %5 %6 %7 %8 %9
        del _Cache_%TempCacheName%_%_rand%.cmd
        set Loaded_%TempCacheName%=
        set TempCacheName=
        set _rand=
        goto :EOF
    )
    goto :EOF
)
goto :EOF
::$Library

Comments