Batch File Cheat Sheet
Help
Make sure that the Sample.html file and the selenium-server.jar. Visit Intellipaat for a comprehensive DevOps Training and be a certified DevOps Engineer! Download a Printable PDF of the Cheat Sheet. We have covered all the basics of Jenkins in this cheat sheet. With LSF you would view the file ADMINusage in your home directory. With Slurm you can run the command “hpcusage” to view your available and used resources. LSF to Slurm Batch Script Reference Note: Slurm submission scripts require a shell declaration as the first line. They will not run without this. Linux Command-Line Cheat Sheet. Operating Systems. Unix Tip: Use your Unix scripting skills to write a batch file. The solution was to whip up a batch file that mapped the drives.
help commandNamecommandName /?help --> show all system commandsAll System Commands
helpClear Screen
Clear ScreenclsShortcuts
TabAutocompletearrow keys up/downback/forward history commandsControl + Cstops current commandALT + F7clears the historyF7shows the historyDirectory Make & Remove
mkdirsubdirectorycreates new subdirectorymkdir subdir/subsubdircreates subdir & subdir/subsubdir
Hint: creates it, if they aren't existingrmdirremoves an empty dirrmdir /s directoryremoves directory with contentrmdir /s /q directoryremoves directory with content & skips y/n question
Directory copy
copy src dstcopy filescopy /Y src destcopy files overwrites without quesitonxcopy /s src destcopies dirs and subdirs except empty onesxcopy /YS src destas above without ask to overwritexcopy /YS src serversharecopy to network sharerobocopy /S src destcopies dirs and subdirs except empty onesrobocopy /YS src destas above without ask to overwriteParameters
%0Name of Script%1 ... %9Parameter 1 ... Parameter 9%*prints all parameters as a string(one line)for %%x in (%*) do echo %%xaccess it through a for loop works with > 10 parameters
Pass Parameters to sub script
parameters_script.bat@echo offcall parameters_subscript.bat %*
EXIT /B %errorlevel%parameters_subscript.bat@ECHO off
echo subscript called with %*
EXIT /B 0EXIT /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]=0set array[1]=1
...set value to a specific array elementecho %array[0]%
echo %array[1]%access to array elementsmissing 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 offsetlocal
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 0FINDSTR /L is use search string literally
Navigation
realativeparent directorycd .., cd..subdirectorycd subdirectoyabsolutecd C:Windowscd Windowschange driveZ:Wildcards
? (file?.txt)Windows Bat File Cheat Sheet
one arbitary character* ( *.bat)arbitary characters (0,1 or more)File Show
type fileshow file contentmoreshow file content pagewisesort fileshow file content sortedDirectory show Content
current folderdirabsolute pathdir C:Windowsdir Y:different drivedir /Bonly show file names and directory namesBatch Files User Input
@ echo offset /P name='Input yor name: 'Syntax: set /P var=prompt:echo Hi %name%variable call
Batch Files File Input
@echo offecho Bill > file
echo Gate >> fileset /P filecontent =< file
echo First Line: %filecontent%
Batch File Cheat Sheet Template
set /P var=< fileFOR /f %%f in (file) Do (echo %%f
)REM print each line in file
Variables
setlocalset MY_ENV=abc
Fun Batch File Codes
endlocalOnly active in the current batch file (local)set MY_ENV=abccreates or changes an environment variable that is active in the cmd. (global)setshows the active environment variablesecho %USERNAME%print variable outdefined MY_VARreturns0 => Execution was successful
>0 => Execution failed
IF Control Structures
IF test (command) ELSE (command)SyntaxControl structures For
for {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]cmd.exe use ->%batch script use ->%%Functions
call :print_name Florianecho print_name exitst with %errorlevel%goto :eof:printnameecho Hit %1
exit /B 0 <- REM sets exit status of :print_name
File Rename & Move
rename file1 file2renamef filte1 to file2move file1 dirmove file 1 to dir (path dir/file1)move file1 dir/file2moves and renames file1 to file2 (path: dir/file2) file2 is overwritten from file1move /Y file1 file2moves and overwrites file1 to file2 without askingFile Names
Case-InsensitiveMax. 260 characters incl. invisible terminating null characterspaces and character set 128 - 255 allowedNever <, >, :, ', /, , |, ?, *Hint: A-Z, a-z, 0-9, _, spaces, ( underscore as space )*.bat & *.cmddir /B --> only show file names and directory NamesDirectory Rename & Move
rename dir1 dir2renames dir1 in dir2move dir1 dir2if dir2 exist: dir1 into dir2 (path: dir2/dir1), else rename dir1 to dir2move file1 dir/file2file 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 file2file 2 is deleted. file1 get the name of file2 ans is set at the position of file2 without asking.File Redirects
type file.txt | sortgive output of type to sort
sort < file.txtgive content ( stdin) of file.txtecho 'hi' > file.txtwrite 'hi' (stout) to file.txttype NUL > file.txtcreate empty filerm file 2> errors.txtredirects errors(stderr) ro errors.txtrm file 2> NULdiscard errorsrm file 2>&1redirect stderr to stdoutecho 'you' >> file.txtappend 'you' to file.txtComment
REMPräfix after them the textCalculation
set /a i=0echo i=%i%set /a i=1+1
Bat Cheat Sheet
Operatoren +, -, *, /set /a i+=1 Operatoren +, -, *, /Arrays Way1
set array=1 2 3 4%array%(echo) all Array elementsfor %%a in (%array%) do (
echo%%a
)loop over elementsset array=(and nothing else)unset variable or array
Control Structures: Comparison Operators
OperatorInfoEqualEQUEqualNEQ!=Not equalLSS<LessLEQ<=Less than or equal toGTR>Greater thanGEQ>=Greater than or equalNOT!NotEditors
NotepadNotepad++VisualStudioCodeEditors
NotepadNotepad++VisualStudioCodeAtomTerminals
cmd.exeWindows TerminalConEmucmderBatch Filex Exit / Return
Specify exit code:echo 'abc'exit 0Query error levelecho %ERRORLEVEL%
Windows Batch File Cheat Sheet
Exit/Return codes0 => Execution was successful>0 => Execution fiales with code X
For more information on a specific command, type HELP command-name
Batch File Cheat Sheet Example
| Command | Description |
|---|---|
ASSOC | Displays or modifies file extension associations. |
ATTRIB | Displays or changes file attributes. |
BREAK | Sets or clears extended CTRL+C checking. |
BCDEDIT | Sets properties in boot database to control boot loading. |
CACLS | Displays or modifies access control lists (ACLs) of files. |
CALL | Calls one batch program from another. |
CD | Displays the name of or changes the current directory. |
CHCP | Displays or sets the active code page number. |
CHDIR | Displays the name of or changes the current directory. |
CHKDSK | Checks a disk and displays a status report. |
CHKNTFS | Displays or modifies the checking of disk at boot time. |
CLS | Clears the screen. |
CMD | Starts a new instance of the Windows command interpreter. |
COLOR | Sets the default console foreground and background colors. |
COMP | Compares the contents of two files or sets of files. |
COMPACT | Displays or alters the compression of files on NTFS partitions. |
CONVERT | Converts FAT volumes to NTFS. |
COPY | Copies one or more files to another location. |
DATE | Displays or sets the date. |
DEL | Deletes one or more files. |
DIR | Displays a list of files and subdirectories in a directory. |
DISKCOMP | Compares the contents of two floppy disks. |
DISKCOPY | Copies the contents of one floppy disk to another. |
DISKPART | Displays or configures Disk Partition properties. |
DOSKEY | Edits command lines, recalls Windows commands, and creates macros. |
DRIVERQUERY | Displays current device driver status and properties. |
ECHO | Displays messages, or turns command echoing on or off. |
ENDLOCAL | Ends localization of environment changes in a batch file. |
ERASE | Deletes one or more files. |
EXIT | Quits the CMD.EXE program (command interpreter). |
FC | Compares two files or sets of files, and displays the differences between them. |
FIND | Searches for a text string in a file or files. |
FINDSTR | Searches for strings in files. |
FOR | Runs a specified command for each file in a set of files. |
FORMAT | Formats a disk for use with Windows. |
FSUTIL | Displays or configures the file system properties. |
FTYPE | Displays or modifies file types used in file extension associations. |
GOTO | Directs the Windows command interpreter to a labeled line in a batch program. |
GPRESULT | Displays Group Policy information for machine or user. |
GRAFTABL | Enables Windows to display an extended character set in graphics mode. |
HELP | Provides Help information for Windows commands. |
ICACLS | Display, modify, backup, or restore ACLs for files and directories. |
IF | Performs conditional processing in batch programs. |
LABEL | Creates, changes, or deletes the volume label of a disk. |
MD | Creates a directory. |
MKDIR | Creates a directory. |
MKLINK | Creates Symbolic Links and Hard Links |
MODE | Configures a system device. |
MORE | Displays output one screen at a time. |
MOVE | Moves one or more files from one directory to another directory. |
OPENFILES | Displays files opened by remote users for a file share. |
PATH | Displays or sets a search path for executable files. |
PAUSE | Suspends processing of a batch file and displays a message. |
POPD | Restores the previous value of the current directory saved by PUSHD. |
PRINT | Prints a text file. |
PROMPT | Changes the Windows command prompt. |
PUSHD | Saves the current directory then changes it. |
RD | Removes a directory. |
RECOVER | Recovers readable information from a bad or defective disk. |
REM | Records comments (remarks) in batch files or CONFIG.SYS. |
REN | Renames a file or files. |
RENAME | Renames a file or files. |
REPLACE | Replaces files. |
RMDIR | Removes a directory. |
ROBOCOPY | Advanced utility to copy files and directory trees |
SET | Displays, sets, or removes Windows environment variables. |
SETLOCAL | Begins localization of environment changes in a batch file. |
SC | Displays or configures services (background processes). |
SCHTASKS | Schedules commands and programs to run on a computer. |
SHIFT | Shifts the position of replaceable parameters in batch files. |
SHUTDOWN | Allows proper local or remote shutdown of machine. |
SORT | Sorts input. |
START | Starts a separate window to run a specified program or command. |
SUBST | Associates a path with a drive letter. |
SYSTEMINFO | Displays machine specific properties and configuration. |
TASKLIST | Displays all currently running tasks including services. |
TASKKILL | Kill or stop a running process or application. |
TIME | Displays or sets the system time. |
TITLE | Sets the window title for a CMD.EXE session. |
TREE | Graphically displays the directory structure of a drive or path. |
TYPE | Displays the contents of a text file. |
VER | Displays the Windows version. |
VERIFY | Tells Windows whether to verify that your files are written correctly to a disk. |
VOL | Displays a disk volume label and serial number. |
XCOPY | Copies files and directory trees. |
WMIC | Displays WMI information inside interactive command shell. |
