Incomplete sheet
This sheet is incomplete and could use some attention. Please submit code snippet suggestions as an issue or PR here.
Windows Batch
Scripting language for Windows Batch files.
Details
To write comments, start the line with :: or REM.
For in-line comments, use & :: or & REM.
Command-line argument handling
| Action |
Code |
Details |
|
Print _n_th parameter
|
|
|
|
Check equality of _n_th parameter
|
if %n%==1 echo "Arg is 1!"
|
|
Variables
By default, variables are global!
For local declaration and usage of variables, encapsulate them in SETLOCAL and ENDLOCAL.
| Action |
Code |
Details |
|
Declare variable named name with value value
|
|
|
|
Declare string variable
|
|
|
|
Declare empty string variable
|
|
|
|
Declare numeric variable named name with value value
|
|
|
|
Get value of variable named name
|
|
Environment variables are just variables. |
|
Get PATH environment variable
|
|
|
|
Test of variable exists
|
set var1 = 2
if defined var1 echo "Variable var1 exists!"
|
|
Control flow
| Action |
Code |
Details |
|
If
|
if %status% == 200 echo OK
|
|
|
If-else
|
if %status% == 200 (echo OK) else (echo Error)
|
|
|
For-loop over range [a, b] with stepsize s
|
for /L %%var in (a,s,b) do echo %%var
|
|
|
Pause until user presses a key
|
|
|
|
Pause until n seconds have elapsed or user presses a key
|
|
|
|
End execution of script
|
|
|
|
End execution of script
|
|
|
Number handling
| Action |
Code |
Details |
|
Add
|
|
|
|
Subtract
|
|
|
|
Multiply
|
|
|
|
Divide
|
|
|
|
Modulus
|
|
|
String handling
| Action |
Code |
Details |
|
Number of characters (length)
|
|
No standard function available |
|
Concatenate strings
|
set w = world
set /a y = 2024
set msg = Hello %w% in %y%!
|
|
|
Test for empty string
|
|
|
|
Parse as integer
|
set /a count = %countstr%
|
|
Output
| Action |
Code |
Details |
|
Clear console
|
|
|
Standard output
| Action |
Code |
Details |
|
Disable command prompts
|
|
|
|
Print string
|
|
|
Filesystem handling
| Action |
Code |
Details |
|
Check if file exists
|
if exist C:\file.txt echo "File exists"
|
|