bat(Batch) batch processing

This article mainly introduces the following functions:

  • Define string variables
  • Define integer variables
  • Do not print the echo of this command
  • Set delay variable
  • String cutting
  • Loop processing
  • if judgment
  • Execute script
  • Abnormal exit program

Example demonstration:

rem starting with rem indicates that this line is a comment

rem @echo off means to turn off the echo of all commands (including the command itself) after executing this command.

@echo off

rem defines string variables

set GRAPHICS_CARD_TYPE=wangzhe,lianmeng,huoxian

rem print string variable

echo 'GRAPHICS_CARD_TYPE:'%GRAPHICS_CARD_TYPE%

rem sets local to delayed expansion. In fact, it is: delay variable

setlocal enabledelayedexpansion
set string=%GRAPHICS_CARD_TYPE%

rem:split indicates a custom loop body, indicating the start of the split loop

rem tokens=1, * indicates the first element 1 and others after the string is cut, delims=, indicates that the string is divided by,

rem %%i represents the first element obtained after looping. Variables should be represented by double quotes + left and right percent signs.

:split
for /f "tokens=1,* delims=," %%i in ("%string%") do (
    echo %%i
    set string=%%j

    rem defines an integer variable
    set /a count=0
    set /a maxnum=5

    rem: loop represents a custom loop body, indicating the start of the loop loop
    : loop

    rem executes the embedded bat script
    call 1_git_clone.bat 

    rem %errorlevel% represents the execution result of the above command. If the execution exception returns 0
    if %errorlevel% neq 0 (
        echo %errorlevel%
        set /a count=%count%+1
        echo %count%

        The rem variable should be represented by double quotes + left and right percent signs.

        rem LEQ表示小于
        if "%count%" LEQ "%maxnum%" (
            echo %count%
            echo Error occured. Retry count %count%.

            rem sets sleep waiting time
            TIMEOUT /T 30

            rem returns to the beginning of the loop body
            goto loop                         
        ) else (
            echo Maximum retry exceeded.

            rem exit means exiting the program abnormally
            exit /b %errorlevel%
        )
    )

    echo %%i
)

if not "%string%"=="" ( rem returns     goto split
    at the beginning of the split loop body )

Guess you like

Origin blog.csdn.net/xch622114/article/details/131327292