Show Menu
Cheatography

A How to manual with hints

Help

help comman­dName
comman­dName /?
help --> show all system commands

All System Commands

help

Clear Screen

Clear Screen
cls

Shortcuts

Tab
Autoco­mplete
arrow keys up/down
back/f­orward history commands
Control + C
stops current command
ALT + F7
clears the history
F7
shows the history

Directory Make & Remove

mkdir
subdir­ectory
creates new subdir­ectory
mkdir subdir­/su­bsubdir
creates subdir & subdir­/su­bsubdir
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 \\serv­er­\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

parame­ter­s_s­cri­pt.bat
@echo off
call parame­ter­s_s­ubs­cri­pt.bat %*
EXIT /B %error­level%
parame­ter­s_s­ubs­cri­pt.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

Double­Cli­ckE­xec­ution

@ECHO off
setlocal

REM check for intera­ctive sesseion
SET intera­ctive=0
ECHO %CMDCM­DLINE% | FINDSTR /L %COMSPEC% >NUL 2>&1
IF %ERROR­LEVEL% EQ SET intera­ctive=1

ECHO do work

REM pause if intera­ctive (doubl­e-c­licked execution)
IF "­%in­era­cti­ve%­" == "­0" PAUSE
EXIT /B 0
FINDSTR /L is use search string literally
 

Navigation

realative
parent directory
cd .., cd..
 
subdir­ectory
cd subdir­ectoy
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=pr­ompt:
echo Hi %name%
variable call

Batch Files File Input

@echo off
echo Bill > file
echo Gate >> file
set /P fileco­ntent =< file
echo First Line: %filec­ontent%
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 enviro­nment variable that is active in the cmd. (global)
set
shows the active enviro­nment 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 {%%|%}­<Va­ria­ble> in (<S­et>) do <Co­mma­nd> [<C­omm­and­Lin­eOp­tio­ns>]
cmd.exe use ->%
batch script use ->%%

Functions

call :print­_name Florian
echo print_name exitst with %error­level%
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 overwr­itten from file1
move /Y file1 file2
moves and overwrites file1 to file2 without asking
 

File Names

Case-I­nse­nsitive
Max. 260 characters incl. invisible termin­ating 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/d­ir1), 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/N­o/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­(st­derr) 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

Calcul­ation

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 Struct­ures: 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++
Visual­Stu­dioCode

Editors

Notepad
Notepad++
Visual­Stu­dioCode
Atom

Terminals

cmd.exe
Windows Terminal
ConEmu
cmder

Batch Filex Exit / Return

Specify exit code:
echo "abc"
exit 0
Query error level
echo %ERROR­LEVEL%
Exit/R­eturn codes
0 => Execution was successful
>0 => Execution fiales with code X
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          CMD Utils Cheat Sheet
          Windows Terminal Cheat Sheet

          More Cheat Sheets by ChaosJD

          CleanMethods Cheat Sheet