Practice learning the basics of batch files (. bat)

target

Through practice, learn the basic writing method of bat file.
Main reference tutorials:
http://www.trytoprogram.com/batch-file/
https://www.tutorialspoint.com/batch_script/index.htm

0. First bat file

Open a text editor and enter the following text:

@echo off
echo Hello bat
pause

Save it as a suffix bat file.

Just double click the file to run it:

1. Notes

REM and:: are used to add comments:

@echo off
REM This line is a comment
:: This line is also a comment 
echo Hello bat
pause

2. Some common commands

For example, REM, echo and pause are commands.
More commonly used commands can be referenced https://www.tutorialspoint.com/batch_script/batch_script_commands.htm , each of which has examples.

3. Variables

Variables in bat do not need to be declared. Just assign values with SET.
The name of the variable cannot be a reserved word. For example, the command name above cannot be used as the name of the variable.

Try creating two variables:

@echo OFF

SET var1=hello
ECHO %var1%

SET var2=7
ECHO %var2%

PAUSE

Output:

hello
7

be careful:
Please do not add spaces around the equal sign. For example, SET name=Apple is correct and SET name=Apple is incorrect.

4*. Use / A to evaluate the expression

For example, for the following bat:

@echo OFF

SET var=2+2
ECHO %var%

PAUSE

Will output:

2+2

The right side of the equal sign will be evaluated as an expression only after / A is added:

@echo OFF

SET /A var=2+2
ECHO %var%

PAUSE

The following is output:

4

5*. Check whether a variable exists

The following statement can be used to determine whether "MyVar" exists.

IF DEFINED MyVar (ECHO It IS defined) ELSE (ECHO It is NOT defined)

For example, try to judge "DATE":

@echo OFF

IF DEFINED DATE (ECHO It IS defined) ELSE (ECHO It is NOT defined)

PAUSE

Will output:

It IS defined

Try to judge "yaksue":

@echo OFF

IF DEFINED yaksue (ECHO It IS defined) ELSE (ECHO It is NOT defined)

PAUSE

Will output:

It is NOT defined

6. Global and local variables

By default, all variables are global.
The variables assigned between SETLOCAL and ENDLOCAL are local variables.
For example, the following bat:

@echo OFF

SETLOCAL
SET var=hello
ECHO %var%
ENDLOCAL

PAUSE

Will output:

hello

And if you try to access outside the scope of a variable:

@echo OFF

SETLOCAL
SET var=hello
ENDLOCAL
ECHO %var%

PAUSE

An error will be displayed:

ECHO Is off.

7. If else branch

Check that the two values are equal:

@echo OFF

SET a=1
SET b=2
SET c=2
IF %a%==%b% (echo They are equal) ELSE (echo They are diffrent)
IF %b%==%c% (echo They are equal) ELSE (echo They are diffrent)

PAUSE

Will output:

They are diffrent
They are equal

8. for loop

The syntax is as follows:

FOR %%variable IN list DO Some things

For example:

@echo OFF

FOR %%i IN (1 2 3) DO ECHO %%i

PAUSE

Will print:

1
2
3

/L means in a range. The syntax is as follows:

FOR /L %%variable IN (minimum value, increment, Maximum) Do Some things

For example:

@echo OFF

FOR /L %%i IN (0, 2, 10) DO ECHO %%i

PAUSE

Will output:

0
2
4
6
8
10

You can also cycle through files in the path.
For example:

@echo OFF

FOR %%f IN (D:/test/*) DO @ECHO %%f

PAUSE

It will output all files in the "D:/test /" folder

9. Function

Use the: function to define the function, and use the CALL: function to CALL the function.

For example, call a function that can perform multiplication:

@echo OFF

CALL :test_function 3, 7
EXIT /B 0

:test_function
SET /A result=%~1*%~2
ECHO %result%
PAUSE

EXIT /B 0

Output:

21

But now the experiment has come down.
It seems that the "function" in bat is more like "goto"?
For example, the following bat:

@echo OFF

CALL :f1 

echo beforef1

:f1
ECHO f1

:f2
ECHO f2
CALL :f4

:f3
ECHO f3

:f4
ECHO f4
PAUSE

Will output:

f1
f2
f4

If exit / B% errorlevel% is added to f1:

@echo OFF

CALL :f1 

echo beforef1

:f1
ECHO f1
EXIT /B %ERRORLEVEL%

:f2
ECHO f2
CALL :f4

:f3
ECHO f3

:f4
ECHO f4
PAUSE

When f1 is executed, it will leave the program directly.
In this case, can't bat actually implement real functions?

Keywords: bash batch

Added by toivo on Wed, 29 Dec 2021 05:04:21 +0200