Personal web page: Emmanuel Branlard

DOS - Basics and useful commands

Batch scripts

Setting a variable

set BRANCH=trunk
Arguments: %1, %2 Characters are escaped with ^, e.g. ^| and ^(:

for /f "tokens=*" %%i in ('make -v ^| find "versoin"') do set version=%%i

Variable manipulations

Extracting the last character from a variable

if "%REVISION_RANGE:~-1%"=="M" ( echo Error: Repository not clean! )

Functions


call :MyFun
echo After call
goto end

:MyFun
echo Function Call
goto eof

:end
echo Done

Storing output of a command into a variable


for /f "tokens=*" %%i in ('make -v ^| find "versoin"') do set version=%%i

Getting input from user


set /p value="Enter a value: "

For loops


for /f "tokens=*" %%i in ('make -v ^| find "versoin"') do set version=%%i

If statements


if not "%ERRORLEVEL%"=="0" (
  echo [FAIL] The command failed.
) else (
  echo [ OK ]
)

Copy


xcopy %EMPTYDIR% %TARGETDIR% /E /K /Y /D /Q

copy source destination

launching external scripts / programs


start /min MyScript.bat ^& exit