Batch file basics

Disable echo:

@echo off

Loops over fixed values:

for %%a in (a b c) do (
    echo %%a


set somevar=myvalue
echo %somevar%
echo %temp%
echo first param=%1
echo second param=%2

Check file existence:

if exist somefile.ext del somefile.ext
if not exist somefile.ext echo no file

Check variable existence:

if defined somevar echo somevar=%somevar%

Numeric comparison:

if 24 gtr 30 (echo GREATER) else echo LESSEQ
rem EQU equal to
rem NEQ not equal to
rem LSS less than
rem LEQ less than or equal to
rem GTR greater than
rem GEQ greater than or equal to


set /p name=What is your name? 
echo Your name is %name%


goto :mylabel
echo blah blah
echo Just skipped blah blah

Numeric expression in variable:

set /a num=0
set /a num=%num%+1
if %num% EQU 10 (goto :next) else (echo %num%)
goto :numbers
echo done



Pass all arguments to another executable:

set args=%1
if [%1] == [] goto done
set args=%args% %1
goto start
.\SomeExecutable.exe %args%

String comparison:

@echo off
set /p ans=Are you sure? 
if “%ans%” == “y” goto :dowork
if “%ans%” == “Y” goto :dowork
goto :eof
echo Work is being done…

Leave a Reply

Your email address will not be published.