Index

batch scripts

don't echo all statements as they are executed
@echo off
comments
rem preferred comment syntax
:: alternative comment syntax

jobs

run job in background
START /B CMD /C CALL "foo.bat" [args [...]]
same without output
START /B CMD /C CALL "foo.bat" [args [...]] >NUL 2>&1
run in separate window
START CMD /C CALL "foo.bat" [args [...]]
run in separate window, keep window open afterwards
START CMD /C CALL "foo.bat" [args [...]] ^& PAUSE
run in minimized window
START /MIN "foo.bat" [args [...]]

variables

setting a variable
set foo=bar
echo %foo%
basic arithmatic
set /A foo=1+1
getting user input
set /p variable=message
set /p "variable=message with spaces"
show all variables
set
common built in variables
%CD%          = current directory
%__CD__%      = current directory, terminated with a trailing backslash
%__APPDIR__%  = directory path to the current application .exe, terminated with a trailing backslash
%TEMP%        = temp dir
%PATH%        = system path
%DATE%        = current date
%RANDOM%      = random integer value
make variable changes script local
setlocal
end local scope, variable changes are now global
endlocal
script file
%0
script parameters
%1 ... %9
shifting out a script parameter
shift
remove quotes
%~[0-9]
show full file path
%~f[0-9]
last part of path
%~nx[0-9]
show dir where script lives
%~dp0

control flow

if else example: test if a command line flag is set
IF "%1"=="-c" (
	ECHO "flag -r"
) ELSE (
	echo "no flag"
)

tutorials

http://steve-jansen.github.io/guides/windows-batch-scripting/part-1-getting-started.html
http://www.trytoprogram.com/batch-file/
https://www.tutorialspoint.com/batch_script/index.htm