Batch is used in [[:windows|Windows]] console programming.
@echo off
set name=John
echo Mein Name ist %name%.
@echo off
set /p name=Gib deinen Namen ein:
echo Hallo %name%!
@echo off
set /p age=Gib dein Alter ein:
if %age% geq 18 (
echo Du bist volljährig.
) else (
echo Du bist minderjährig.
)
@echo off
for /L %%i in (1,1,5) do (
echo Schleifendurchlauf Nr. %%i
)
@echo off
set filename=myfile.txt
set content=
for /f "usebackq delims=" %%i in ("%filename%") do (
call set "content=%%content%% %%i"
)
echo File content: %content%
@echo off
goto main
rem Codeblock als "Funktion"
:MeineFunktion
echo Das ist meine Funktion.
echo Ich kann hier beliebigen Code platzieren.
goto :eof
rem Hauptprogramm
:main
echo Start des Hauptprogramms.
echo Hier passiert etwas ...
goto MeineFunktion
echo Hier geht es nach der Funktion weiter.
:eof
echo Ende des Skripts.
// arguments
echo $1 $2
// variables
set uri=video.mp4
echo $uri
// prompt
set /p uri=Enter URI:
// save output of program (whoami) into variable
for /f "delims=" %a in ('whoami') do set user=%a
for %%x in (%*) do echo %%x
=====Input=====
SET /P "Continue [y/n]>" %confirm%
FINDSTR /I "^(y|n|yes|no)$" > NUL || GOTO: confirm
=====Error level=====
Ein Programm hat immer einen Rückgabewert. Zumeist wird 0 zurückgegeben um zu signalisieren das alles erfolgreich erledigt wurde. Im Fehlerfall wird eine 1 zurückgegeben. Manche Programme verwenden auch Fehlercodes. Solange die Rückgabe nicht 0 ist kann man von einem Fehler ausgehen.
ping -n 1 example.local | findStr "TTL" > nul
if %errorlevel% >= 1 (
echo host not reachable
) else (
echo host reachable
)
=====Labels=====
echo "static, default"
set /p type=Choose:
if %type% == "default" (
goto default
) else (
if %type% == "static" (
goto static
) else (
goto none
)
)
:default
goto done
:static
goto done
:none
goto done
:done
echo Done
=====Loops=====
FOR %I IN (%USERPROFILE%\*) DO @ECHO %I
Looping Through Files
for /F %a in ('dir /B test') do echo %a
Looping Through Directories
FOR /D %I IN (%USERPROFILE%\*) DO @ECHO %I
Recursively loop through files in all subfolders of the %TEMP% folder
FOR /R "%TEMP%" %I IN (*) DO @ECHO %I
Recursively loop through all subfolders in the %TEMP% folder
FOR /R "%TEMP%" /D %I IN (*) DO @ECHO %I
Loop thru lines in a file
for /F "tokens=*" %%A in (tmp.txt) do echo %%A
=====Functions=====
@ECHO OFF
SETLOCAL
:: script global variables
SET me=%~n0
SET log=%TEMP%\%me%.txt
:: The "main" logic of the script
IF EXIST "%log%" DELETE /Q %log% >NUL
:: do something cool, then log it
CALL :tee "%me%: Hello, world!"
:: force execution to quit at the end of the "main" logic
EXIT /B %ERRORLEVEL%
:: a function to write to a log file and write to stdout
:tee
ECHO %* >> "%log%"
ECHO %*
EXIT /B 0
=====Parameters=====
SET filepath=%~f1
IF NOT EXIST "%filepath%" (
ECHO %~n0: file not found - %filepath% >&2
EXIT /B 1
)
Optional parameters
SET filepath=%dp0\default.txt
:: the first parameter is an optional filepath
IF EXIST "%~f1" SET filepath=%~f1
=====Interactive script=====
@ECHO OFF
SET interactive=0
ECHO %CMDCMDLINE% | FINDSTR /L %COMSPEC% >NUL 2>&1
IF %ERRORLEVEL% == 0 SET interactive=1
ECHO do work
IF "%interactive%"=="0" PAUSE
EXIT /B 0