Batch File Commands List With Examples
In this tutorial, we are going to see a list of batch file commands with examples. Batch files are batch files that allow Windows users to automate system or program processes. For this purpose, these files contain commands, also called “batch commands”, which can be executed via the command prompt. There are hundreds of batch commands that can be used to automate many tasks.
Batch File Commands List With Examples
VER
Allows you to display the current version of the operating system.
Example:
@echo off VER
Output:
Microsoft Windows [Version 10.0.18362.1016]
DIR
Allows you to display the entire contents of a directory.
Example:
The following example list the contents of c:\example including all files:
@echo off DIR /a c:\example
Output:
Volume in drive C has no label. Volume Serial Number is B48B-AA22 Directory of C:\example 09/14/2021 09:43 AM <dir> . 09/14/2021 09:43 AM <dir> .. 07/27/2021 05:10 PM 118,622 mcq.png 09/13/2021 08:06 PM 15 script.txt 03/16/2021 01:10 PM 322,374 suprabuilder.js 09/14/2021 09:43 AM <dir> tmp 3 File(s) 441,011 bytes 3 Dir(s) 10,017,726,464 bytes free
DEL
Allows you to delete individual files.
Example:
The following example delete “My File.txt”:
@echo off DEL "My File.txt"
MKDIR
Allows you to create a directory.
Example:
The following example create a new directory called “MyFolder”:
@echo off MKDIR C:\Users\StackHowTo\MyFolder
RMDIR
Allows you to delete a directory.
Example:
The following example delete the directory called “MyFolder”:
@echo off RMDIR C:\Users\StackHowTo\MyFolder
RENAME
This command allows you to rename files.
Example:
The following example rename “example.txt” as “test.txt”:
@echo off RENAME example.txt test.txt
REPLACE
This command allows you to replace or overwrite files.
Example:
The following example Update the record.mp3 file in all the folders under C:\records :
@echo off REPLACE "C:\tmp\record.mp3" C:\records /s
Output:
Replacing C:\records\Jan\record.mp3 Replacing C:\records\Feb\record.mp3 Replacing C:\records\Mar\record.mp3
MOVE
This command allows you to rename or move files or directories.
Example:
The following example rename “oldfile.txt” as “newfile.doc” in the current folder:
@echo off MOVE oldfile.txt newfile.doc
Output:


COPY
This command allows you to copy files.
Example:
The following example copy the content from “source.doc” to “newfile.doc” in the current folder:
@echo off COPY source.doc newfile.doc
Output:
1 file(s) copied.
CD
This command allows you to switch to another directory or folder.
Example:
The following example switch to the parent directory:
@echo off CD ..
CHDIR
This command is a synonym for CD.
Example:
The following example switch to the parent directory:
@echo off CHDIR ..
CHKDSK
This command allows you to search for errors on hard disks.
Example:
The following example scan the C drive:
@echo off CHKDSK C:
Output:
The type of the file system is NTFS. Stage 1: Examining basic file system structure ... 879872 file records processed. File verification completed. 36471 large file records processed. 0 bad file records processed. Stage 2: Examining file name linkage ... 33374 reparse records processed. 1156752 index entries processed. Index verification completed.
CLS
This command allows you to delete all the content on the screen.
Example:
@echo off CLS
Output:


COLOR
This command allows you to change the background color of the current console.
Example:
The following example change the color to White on Blue:
@echo off COLOR 17
Output:


To change back to the default terminal color run this command COLOR 07(white on black).
COMP
This command allows you to compare the contents of two or more files.
Example:
The following example compare the contents of file1.txt to file2.txt:
@echo off COMP C:\demo\file1.txt C:\demo\file2.txt
Output:
Comparing file1.txt and file2.txt... Files compare OK
DATE
This command allows you to display and change the system date/time.
Example:
The following example display the system date/time:
@echo off echo %DATE% echo %TIME%
Output:
Tue 09/14/2021 13:33:43.63
ECHO
This command allows you to display messages in the console and activation/deactivation of the command display.
Example:
The following example display the department variable:
@echo off Set _department = 'Tech' ECHO %_department%
Output:
'Tech'
EXIST
This command allows you to check if a file exists.
Example:
The following example check if “filename” exists:
@echo off IF EXIST "filename" ( echo 'File EXIST!' ) ELSE ( echo 'File missing!' )
EXIT
This command allows you to ends a batch file or command prompt.
Example:
The following example exit if the required file “myFile.txt” is missing:
@Echo Off IF NOT EXIST myFile.txt EXIT /b Echo "File Exists!"
FOR
This command allows you to creates a for() loop that polls the commands one after the other at the frequency indicated in parentheses.
Example:
@Echo Off FOR /L %%A IN (1,1,10) DO ( ECHO %%A )
1,1,10 means:
- Start = 1
- Increment per step = 1
- End = 10
Output:
1 2 3 4 5 6 7 8 9 10
IF
This command allows you to integrate conditions within batch files, similar to JavaScript for example.
Example:
The following example exit if the required file “myFile.txt” is missing:
@Echo Off IF NOT EXIST myFile.txt EXIT /b Echo "File Exists!"
REM
All commands coming after REM or :: are considered as comments by the console.
Example:
The following example exit if the required file “myFile.txt” is missing:
@Echo Off :: First comment REM Second comment
GOTO
This command allows you to go directly to a specific location in a batch file.
Example:
The following example jump to ‘next_message’ label and display “Next Message”:
@Echo Off GOTO next_message Echo "This will not display" goto:eof :next_message Echo "Next Message"
Output:
Next Message
PAUSE
The executed batch file is stopped and the console displays the message Press any key to continue….
Example:
@Echo Off PAUSE
Output:
Press any key to continue . . .
CALL
This command allows you to call a batch file from another batch file.
Example:
The following example call SecondScript.bat from the current script.
@Echo Off CALL SecondScript
SET
This command allows you to read and delete variables in the command prompt.
Example:
The /P option allows you to set a variable equal to the value entered by the user.
@echo off Set /P _department=Please enter Department: If /i "%_department%"=="finance" goto finance If /i "%_department%"=="hr" goto hr goto:eof :finance echo You have chosen the Finance department goto:eof :hr echo You have chosen the HR department goto:eof
Output:


TITLE
This command allows you to change the title of the command prompt window.
Example:
The following example change the title of the command prompt to “Welcome to StackHowTo!”.
@Echo Off TITLE "Welcome to StackHowTo!"
Output:


START
This command allows you to start some programs or some commands.
Example:
The following example will start two programs called Notepad and Calculator.
@Echo Off START notepad.exe START calc.exe
Output:


SHUTDOWN
This command allows you to closes the session, restarts the computer, or shuts it down.
Example:
The following example will shut down your computer in 60 seconds.
@Echo Off SHUTDOWN /s /t 60 /c
FIND
This command allows you to search for one or more files by using character input.
Example:
The following example will search for “hello” in myFile.txt.
@Echo Off FIND "hello" myFile.txt
MORE
This command allows you to display data page by page on the screen.
Example:
The following example will display the content of myFile.txt page by page on the screen.
@Echo Off MORE myFile.txt
TYPE
This command allows you to display the contents of text files.
Example:
The following example will display the content of myFile.txt.
@Echo Off TYPE myFile.txt
PING
This command allows you to test the connection with another device.
Example:
The following example will ping a website(example.com) 5 times.
@Echo Off PING -n 5 www.example.com
HOSTNAME
This command allows you to display the computer’s name.
Example:
@Echo Off HOSTNAME