0% found this document useful (0 votes)
371 views3 pages

Batch Cheat Sheet: by Via

This document provides a cheat sheet for common batch scripting commands and concepts in 3 sentences or less: It outlines various commands for navigating directories, manipulating files and folders, using variables and arrays, and controlling program flow. Shortcuts for navigation and editing are included, as well as best practices for passing parameters between scripts and checking for double-click execution. Wildcards, file properties, and renaming/moving files are also summarized concisely.

Uploaded by

Luis Julio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
371 views3 pages

Batch Cheat Sheet: by Via

This document provides a cheat sheet for common batch scripting commands and concepts in 3 sentences or less: It outlines various commands for navigating directories, manipulating files and folders, using variables and arrays, and controlling program flow. Shortcuts for navigation and editing are included, as well as best practices for passing parameters between scripts and checking for double-click execution. Wildcards, file properties, and renaming/moving files are also summarized concisely.

Uploaded by

Luis Julio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Batch Cheat Sheet

by ChaosJD (ChaosJD) via cheatography.com/99440/cs/21487/

Help Directory copy (cont) Arrays Way2 (cont)

help comman​dName xcopy /YS src as above with​out ask to missing loop over elements: indexed
comman​dName /? dest overwrite
xcopy /YS src copy to network share Functions: label and goto
help --> show all system commands
\\serv​er​\share echo "Hi:"
All System Commands robocopy /S src copies dirs and subdirs goto :good
dest except empty ones :bad
help
robocopy /YS as above without ask to echo "I don't like scripting"
Clear Screen src dest overwrite goto :eof
:good
Clear Screen cls
Parameters echo "I like scripting"
%0 Name of Script goto :eof
Shortcuts

Tab Autoco​mplete %1 ... %9 Parameter 1 ... Parameter


Double​Cli​ckE​xec​ution
9
arrow keys back/f​orward history
@ECHO off
up/down commands %* prints all parameters as a
setlocal
string
Control + C stops current command REM check for intera​ctive sesseion
(one line) access it through a for loop
ALT + F7 clears the history SET intera​ctive=0
for %%x in works with > 10
F7 shows the history ECHO %CMDCM​DLINE% | FINDSTR /L
(%*) do echo parameters
%COMSPEC% >NUL 2>&1
%%x
Directory Make & Remove IF %ERROR​LEVEL% EQ SET intera​‐
ctive=1
mkdir creates new subdir​ectory Pass Parameters to sub script
ECHO do work
subdir​‐
parame​ter​s_s​cri​‐ @echo off REM pause if intera​ctive (doubl​e-c​licked
ectory
pt.bat call parame​ter​s_s​ubs​‐ execution)
mkdir creates subdir & subdir​/su​‐ cri​pt.bat %* IF "​%in​era​cti​ve%​" == "​0" PAUSE
subdir​/su​‐ bsubdir EXIT /B %error​level% EXIT /B 0
bsubdir Hint: creates it, if they aren't
parame​ter​s_s​‐ @ECHO off FINDSTR /L is use search string literally
existing
ubs​cri​pt.bat echo subscript called
rmdir removes an empty dir with %* Navigation
rmdir /s removes directory with EXIT /B 0
realative parent cd .., cd..
directory content EXIT /B specifies to exit the current batch directory
rmdir /s /q removes directory with scropt instead of cmd.exe. If executed from
subdir​ectory cd subdir​‐
directory content & skips y/n question outside a batch script, it will quit cmd.exe
ectoy
absolute cd
Directory copy Arrays Way2
C:\Windows
copy src copy files set set value to a specific array
cd \Windows
dst array[0]=0 element
set change Z:
copy /Y src copy files overwrites with​out
array[1]=1 drive
dest quesiton
...
xcopy /s copies dirs and subdirs
src dest except empty ones echo access to array elements
%array[0]%
echo
%array[1]%

By ChaosJD (ChaosJD) Published 22nd October, 2020. Sponsored by CrosswordCheats.com


cheatography.com/chaosjd/ Last updated 22nd October, 2020. Learn to solve cryptic crosswords!
Page 1 of 3. http://crosswordcheats.com
Batch Cheat Sheet
by ChaosJD (ChaosJD) via cheatography.com/99440/cs/21487/

Wildcards Variables File Rename & Move (cont)

? one arbitary character setlocal Only active in the move moves and renames file1 to file2
(file?.txt) set current batch file (local) file1 (path: dir/file2) file2 is overwr​itten
* ( *.bat) arbitary characters (0,1 or MY_ENV=abc dir/file2 from file1
more) endlocal move moves and overwrites file1 to file2
set creates or changes an /Y file1 with​out asking
File Show MY_ENV=abc enviro​nment variable file2
that is active in the cmd.
type file show file content
(global) File Names
more show file content page​wise
set shows the active enviro​‐ Case-I​nse​nsitive
sort file show file content sorted
nment variables
Max. 260 characters incl. invisible termin​‐
echo print variable out ating null character
Directory show Content
%USERNAME%
spaces and character set 128 - 255 allowed
current dir
defined returns
folder Never <, >, :, ", /, \, |, ?, *
MY_VAR 0 => Execution was
absolute dir C:\Windows Hint: A-Z, a-z, 0-9, _, spaces, ( underscore
successful
path as space )
>0 => Execution failed
dir Y: different drive *.bat & *.cmd

dir /B only show file names and IF Control Structures dir /B --> only show file names and directory
directory names Names
IF test (command) ELSE Syntax
(command)
Batch Files User Input Directory Rename & Move

@ echo off Syntax: set /P Control structures For rename renames dir1 in dir2
set /P name="Input var=pr​ompt: dir1
for {%%|%}​<Va​ria​ble> in (<S​et>) do <Co​‐
yor name: " dir2
mma​nd> [<C​omm​and​Lin​eOp​tio​ns>]
echo Hi %name% variable call move if dir2 exist: dir1 into dir2 (path:
cmd.exe use ->%
dir1 dir2/d​ir1), else rename dir1 to
batch script use ->​%%
Batch Files File Input dir2 dir2

@echo off move file 2 is deleted. file1 get the


Functions
echo Bill > file file1 name of file2 ans is set at the
call :print​_name Florian
echo Gate >> file dir/file2 position of file2 with asking
echo print_name exitst with %error​level% (Yes/N​o/All)
set /P fileco​ntent =< set /P var=< file
goto :eof move file 2 is deleted. file1 get the
file
echo First Line: :printname /Y file1 name of file2 ans is set at the
%filec​ontent% echo Hit %1 file2 position of file2 with​out asking.
exit /B 0 <- REM sets exit status of :print​‐
FOR /f %%f in (file) REM print each
_name
Do ( line in file
echo %%f
File Rename & Move
)
rename file1 renamef filte1 to file2
file2
move file1 dir move file 1 to dir (path
dir/file1)

By ChaosJD (ChaosJD) Published 22nd October, 2020. Sponsored by CrosswordCheats.com


cheatography.com/chaosjd/ Last updated 22nd October, 2020. Learn to solve cryptic crosswords!
Page 2 of 3. http://crosswordcheats.com
Batch Cheat Sheet
by ChaosJD (ChaosJD) via cheatography.com/99440/cs/21487/

File Redirects Control Struct​ures: Comparison


Operators
type file.txt | give output of type to sort
sort Operator Info Equal
sort < file.txt give content ( stdin) of EQU == Equal
file.txt NEQ != Not equal
echo "​hi" > write "​hi" (stout) to file.txt LSS < Less
file.txt
LEQ <= Less than or equal to
type NUL > create empty file
GTR > Greater than
file.txt
GEQ >= Greater than or equal
rm file 2> redi​rects errors​(st​derr) ro
NOT ! Not
errors.txt errors.txt
rm file 2> NUL discard errors
Editors
rm file 2>&1 redi​rect stderr to stdout
Notepad
echo "​you​" >> append "​you​" to file.txt
Notepad++
file.txt
Visual​Stu​dioCode
Comment
Editors
REM Präfix after them the text
Notepad
Calcul​ation Notepad++

set /a i=0 Visual​Stu​dioCode


echo i=%i% Atom
set /a i=1+1 Operatoren +, -, *, /
Terminals
set /a i+=1 Operatoren +, -, *, /
cmd.exe
Arrays Way1 Windows Terminal
set arra​y=1 2 3 4 ConEmu
%array% (echo) all Array cmder
elements
for %%a in loop over elements Batch Filex Exit / Return
(%array%) do ( Specify exit echo "abc"
echo%%a code: exit 0
)
Query error echo %ERROR​LEVEL%
set array=​(and unset vari​able or level
nothing else) array
Exit/R​eturn 0 => Execution was
codes successful
>0 => Execution fiales with
code X

By ChaosJD (ChaosJD) Published 22nd October, 2020. Sponsored by CrosswordCheats.com


cheatography.com/chaosjd/ Last updated 22nd October, 2020. Learn to solve cryptic crosswords!
Page 3 of 3. http://crosswordcheats.com

You might also like