Web Site
Basic
Adding rem as prefix would make the line to be comment
1
|
rem this is a line of commented text
|
Set/Read Variable
To set a variable in CMD, use set command of syntax set {variable_name}={variable_value}.
No double quote to pass in, everything after = is reflected in the value.
To retrieved variable use two % enclosing the variable name
1
2
3
4
|
set a= " b"
echo %a%
rem e.g. ` " b"`
|
Script basic initializer
Use pushd to switch to the script’s directory and popd to restore the
original directory. Route normal completion and errors to :cleanup; an
exit /b in the script body would bypass cleanup.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@echo off
setlocal
set "SCRIPT_NAME=%~nx0"
set "SCRIPT_DIR=%~dp0"
set "EXIT_CODE=0"
set "PUSHED=0"
echo Script started at: %date% %time%
pushd "%SCRIPT_DIR%"
if errorlevel 1 (
set "EXIT_CODE=1"
goto cleanup
)
set "PUSHED=1"
rem Your script below
rem Set EXIT_CODE before goto cleanup if the script fails.
rem Example: set "EXIT_CODE=1" & goto cleanup
rem Compact error check: your-command || (set "EXIT_CODE=1" & goto cleanup)
:cleanup
if "%PUSHED%"=="1" popd
echo Script ended at: %date% %time% 1>&2
endlocal & exit /b %EXIT_CODE%
|
Related articles:
CMD script initialization