Help
help commandName |
commandName /? |
help --> show all system commands |
Shortcuts
Tab |
Autocomplete |
arrow keys up/down |
back/forward history commands |
Control + C |
stops current command |
ALT + F7 |
clears the history |
F7 |
shows the history |
Directory Make & Remove
mkdir subdirectory |
creates new subdirectory |
mkdir subdir/subsubdir |
creates subdir & subdir/subsubdir Hint: creates it, if they aren't existing |
rmdir |
removes an empty dir |
rmdir /s directory |
removes directory with content |
rmdir /s /q directory |
removes directory with content & skips y/n question |
Directory copy
copy src dst |
copy files |
copy /Y src dest |
copy files overwrites without quesiton |
xcopy /s src dest |
copies dirs and subdirs except empty ones |
xcopy /YS src dest |
as above without ask to overwrite |
xcopy /YS src \\server\share |
copy to network share |
robocopy /S src dest |
copies dirs and subdirs except empty ones |
robocopy /YS src dest |
as above without ask to overwrite |
Parameters
%0 |
Name of Script |
%1 ... %9 |
Parameter 1 ... Parameter 9 |
%* |
prints all parameters as a string |
(one line) for %%x in (%*) do echo %%x |
access it through a for loop works with > 10 parameters |
Pass Parameters to sub script
parameters_script.bat |
@echo off call parameters_subscript.bat %* EXIT /B %errorlevel% |
parameters_subscript.bat |
@ECHO off echo subscript called with %* EXIT /B 0 |
EXIT /B specifies to exit the current batch scropt instead of cmd.exe. If executed from outside a batch script, it will quit cmd.exe
Arrays Way2
set array[0]=0 set array[1]=1 ... |
set value to a specific array element |
echo %array[0]% echo %array[1]% |
access to array elements |
missing loop over elements: indexed |
Functions: label and goto
echo "Hi:" goto :good |
:bad echo "I don't like scripting" goto :eof |
:good echo "I like scripting" goto :eof |
DoubleClickExecution
@ECHO off
setlocal
REM check for interactive sesseion
SET interactive=0
ECHO %CMDCMDLINE% | FINDSTR /L %COMSPEC% >NUL 2>&1
IF %ERRORLEVEL% EQ SET interactive=1
ECHO do work
REM pause if interactive (double-clicked execution)
IF "%ineractive%" == "0" PAUSE
EXIT /B 0 |
FINDSTR /L is use search string literally
|
|
Navigation
realative |
parent directory |
cd .., cd.. |
|
subdirectory |
cd subdirectoy |
absolute |
cd C:\Windows |
|
|
cd \Windows |
change drive |
Z: |
|
Wildcards
? (file?.txt) |
one arbitary character |
* ( *.bat) |
arbitary characters (0,1 or more) |
File Show
type file |
show file content |
more |
show file content pagewise |
sort file |
show file content sorted |
Directory show Content
current folder |
dir |
absolute path |
dir C:\Windows |
dir Y: |
different drive |
dir /B |
only show file names and directory names |
Batch Files User Input
@ echo off set /P name="Input yor name: " |
Syntax: set /P var=prompt: |
echo Hi %name% |
variable call |
Batch Files File Input
@echo off echo Bill > file echo Gate >> file |
set /P filecontent =< file echo First Line: %filecontent% |
set /P var=< file |
FOR /f %%f in (file) Do ( echo %%f ) |
REM print each line in file |
Variables
setlocal set MY_ENV=abc endlocal |
Only active in the current batch file (local) |
set MY_ENV=abc |
creates or changes an environment variable that is active in the cmd. (global) |
set |
shows the active environment variables |
echo %USERNAME% |
print variable out |
defined MY_VAR |
returns 0 => Execution was successful >0 => Execution failed |
IF Control Structures
IF test (command) ELSE (command) |
Syntax |
Control structures For
for {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>] |
cmd.exe use ->% |
batch script use ->%% |
Functions
call :print_name Florian |
echo print_name exitst with %errorlevel% |
goto :eof |
:printname echo Hit %1 exit /B 0 <- REM sets exit status of :print_name |
File Rename & Move
rename file1 file2 |
renamef filte1 to file2 |
move file1 dir |
move file 1 to dir (path dir/file1) |
move file1 dir/file2 |
moves and renames file1 to file2 (path: dir/file2) file2 is overwritten from file1 |
move /Y file1 file2 |
moves and overwrites file1 to file2 without asking |
|
|
File Names
Case-Insensitive |
Max. 260 characters incl. invisible terminating null character |
spaces and character set 128 - 255 allowed |
Never <, >, :, ", /, \, |, ?, * |
Hint: A-Z, a-z, 0-9, _, spaces, ( underscore as space ) |
*.bat & *.cmd |
dir /B --> only show file names and directory Names |
Directory Rename & Move
rename dir1 dir2 |
renames dir1 in dir2 |
move dir1 dir2 |
if dir2 exist: dir1 into dir2 (path: dir2/dir1), else rename dir1 to dir2 |
move file1 dir/file2 |
file 2 is deleted. file1 get the name of file2 ans is set at the position of file2 with asking (Yes/No/All) |
move /Y file1 file2 |
file 2 is deleted. file1 get the name of file2 ans is set at the position of file2 without asking. |
File Redirects
type file.txt | sort |
give output of type to sort |
sort < file.txt |
give content ( stdin) of file.txt |
echo "hi" > file.txt |
write "hi" (stout) to file.txt |
type NUL > file.txt |
create empty file |
rm file 2> errors.txt |
redirects errors(stderr) ro errors.txt |
rm file 2> NUL |
discard errors |
rm file 2>&1 |
redirect stderr to stdout |
echo "you" >> file.txt |
append "you" to file.txt |
Comment
REM |
Präfix after them the text |
Calculation
set /a i=0 echo i=%i% |
set /a i=1+1 Operatoren +, -, *, / |
set /a i+=1 Operatoren +, -, *, / |
Arrays Way1
set array=1 2 3 4
|
%array% |
(echo) all Array elements |
for %%a in (%array%) do ( echo%%a ) |
loop over elements |
set array=(and nothing else) |
unset variable or array |
Control Structures: Comparison Operators
Operator |
Info |
Equal |
EQU |
== |
Equal |
NEQ |
!= |
Not equal |
LSS |
< |
Less |
LEQ |
<= |
Less than or equal to |
GTR |
> |
Greater than |
GEQ |
>= |
Greater than or equal |
NOT |
! |
Not |
Editors
Notepad |
Notepad++ |
VisualStudioCode |
Editors
Notepad |
Notepad++ |
VisualStudioCode |
Atom |
Terminals
cmd.exe |
Windows Terminal |
ConEmu |
cmder |
Batch Filex Exit / Return
Specify exit code: |
echo "abc" exit 0 |
Query error level |
echo %ERRORLEVEL% |
Exit/Return codes |
0 => Execution was successful >0 => Execution fiales with code X |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by ChaosJD