In das Verzeichnis des Batch-Skripts wechseln:
cd /D %~d0%~p0
IF syntax:
IF Bedingung ( Befehl1 Befehl2 ) IF "%LOCKPID%" == "%PID%" ( ... )
Zwischen Bedingung und der Klammer "(" muss ein Leerzeichen sein.
Rote Schrift auf schwarzem Grund:
color 0c
Variable auf Existenz prüfen:
if not defined Var1keine Prozentzeichen um die Variable
Alternativ:
if NOT [%Var1%]==[] ( ) else ...
Lesen von Benutzereingaben:
set /P v=Lesen aus Datei:
set /P v= < tmp.txt
Auf übergebene Parameter prüfen:
if not [%1]==[] echo %1 if not [%1]'==' echo %1
Ausgaben/Fehler unterdrücken:
stdout unterdrücken:
tool.exe >NULstdout und stderr unterdrücken:
tool.exe >NUL 2>&1
Pfad in Variable einlesen:
set PFAD=%CD%
Wichtige vordefinierte Variablen:
%CD% - expandiert zum aktuellen Verzeichnisnamen. %DATE% - expandiert zum aktuellen Datum unter Verwendung desselben Formats wie der DATE-Befehl. %TIME% - expandiert zur aktuellen Zeit unter Verwendung desselben Formats wie der TIME-Befehl. %RANDOM% - expandiert zu einer zufällig gewählten Dezimalzahl zwischen 0 und 32767. %ERRORLEVEL% - expandiert zum aktuellen ERRORLEVEL-Wert. %CMDEXTVERSION% - expandiert zur Versionsnummer der aktuellen Erweiterungen für den Befehlsinterpreter. %CMDCMDLINE% - expandiert zur ursprünglichen Befehlszeile, die den Befehlsinterpreter aufgerufen hat.
Benutzereingabe anfragen:
set /P choice= Do you want to [a]bort or [u]nlock the directory? if /I "%choice%"=="a" goto abort if /I "%choice%"=="c" goto continue echo Invalid choice: [%choice%] goto :eof :abort exit /b 1 :continue @echo. Continuing...
Befehl auf erfolgreiche Ausführung prüfen:
mkdir "%OUT_DIR%" || ( @echo Could not create output directory @set ERROR=Could not create output directory @goto end )
Sonderzeichen aus einer Variablen entfernen
@rem ########################################################################## @rem @rem Clean variable name @rem @set str=%VAR% @rem Trim Quotes - Remove surrounding quotes via FOR command @for /f "useback tokens=*" %%a in ('%str%') do @set str=%%~a @REM @rem Trim Right - Trim spaces from the end of a string via "FOR" command @REM @rem Trimming spaces at the end of a variable seems a little tricky. The following example shows how to use a FOR loop to trim up to 31 spaces from the end of a string. It assumes that Delayed Expansion is enabled. @for /l %%a in (1,1,31) do @if "!str:~-1!"==" " @set str=!str:~0,-1! @rem Trim Left - Trim spaces from the beginning of a string via "FOR" command @for /f "tokens=* delims= " %%a in ("%str%") do @set str=%%a @rem Remove Spaces - Remove all spaces in a string via substitution @set str=%str: =% @rem Replace all ':\' characters by _ @set str=%str::\=_% @rem Replace all ':/' characters by _ @set str=%str::/=_% @rem Replace all '.' characters by _ @set str=%str:.=_% @rem Replace all '\' characters by _ @set str=%str:\=_% @rem Replace all '/' characters by _ @set str=%str:/=_% @rem Replace all ':' characters by _ @set str=%str::=_% @rem Replace all ',' characters by _ @set str=%str:,=_% @set VAR=%str% @set str= @rem ##########################################################################