Windows OS

Batch File Commands: A Comprehensive Guide to Common Commands

Windows operating systems employ batch files, which are scripts, to automate processes and run a series of commands. These commands are typically used in the Windows Command Prompt or in batch (.bat) files.

Here is a comprehensive guide to common batch file commands with simple example code:

ECHO:

This command is used to display text on the command prompt. The text can be enclosed in quotation marks if it contains spaces.

Open Notepad or any other editing tool and enter the below code. Save the file to the desired working directory using the .bat extension (ECHO.bat).

ECHO Printing Mobile Details

ECHO IPhone 11 Pro-Max

ECHO Storage: 128GB

ECHO RAM: 16GB

ECHO COLOR: WHITE

Open the command prompt in the Windows operating system, navigate to the working directory, and enter the file name along with the .bat extension. The below output will be printed on the command-line screen:

CLS:

This command clears the command prompt screen. Here is an example of how you can use the CLS command in a batch file:

@echo off

cls

echo Clear Previous Screen

In order to prevent the user from viewing the instructions being performed, this batch file first turns off the command echoing using the @echo off command. After that, the command prompt screen is cleared by applying the cls command. Finally, it displays a message using the echo command.

TITLE:

This command modifies the title text that appears at the top of the command-line window.

TITLE My First Batch File Script

EXIT:

This command terminates the batch file and exits the command prompt.

PAUSE:

This command delays the batch file’s execution while it waits for the user input to press a key. In the given code, in order to prevent the user from seeing commands being run, the first line, @echo off, disables command echoing.

The next three lines, ECHO Sale Amount 1: 500, ECHO Sale Amount 2: 250, and ECHO Press Enter to see the Total, display the sale amounts and a message prompting the user to press Enter to see the total.

The batch file’s fifth line, PAUSE, pauses execution while it waits for a key to be pressed by the user. The sixth line, ECHO Total Sale Amount: 750, displays the total sale amount, which is calculated by adding the two sale amounts.

@echo off

ECHO Sale Amount 1: 500

ECHO Sale Amount 2: 250

ECHO Press Enter to see the Total

PAUSE

ECHO Total Sale Amount: 750

Here is the output:

REM:

Adds comments to the batch file for documentation purposes. Comments are not executed. The alternative statement used for comments is the double colon operator “::”.

@ECHO OFF

REM This is a comment using REM command

:: This is a comment using double colon statement

ECHO Hello, I am working with Batch Commands

@ECHO OFF:

With this command, the batch file’s command echoing is disabled. This is beneficial if we choose not to allow the user to be able to see the commands being executed.

@ECHO OFF

REM This is a comment using REM command

:: This is a comment using double colon statement

ECHO Hello, I am working with Batch Commands

FOR:

This command is used to go over a list of objects repeatedly. A list can consist of a range of numbers, filenames, or environment variables. The for loop in the code underneath prints a table of two values.

@echo off

setlocal enabledelayedexpansion

echo TABLE of 2 (TWO)

for /l %%i in (1,1,10) do (

set /a product=2*%%i

echo 2 x %%i = !product!

)

endlocal

MKDIR:

This command creates a new directory or folder at the specified location. With the assistance of the command line, create the directory “MY-Products” using the following code since we have no folder or directory in the working folder after running “MKDir.bat”:

@ECHO OFF

REM Create Directory

MKDIR MY-Products

RMDIR:

An empty directory can be deleted using this command. The alternate command is DEL. Use the code below to delete the MY-Products directory:

@ECHO OFF

REM Remove/Delete Directory

RMDIR MY-Products

Directory Removed Successfully:

COPY:

To move files or directories between two locations, use the COPY command.

Copy one file using the code below:

@ECHO OFF

REM Remove/Delete Directory

COPY myProducts1.txt productsbackup.txt

ECHO Copy Successful

Output:

If the file is missing from the source location, the batch will display an error message on the command screen.

Copy Files and Folder:

Here, we want to copy the entire Products folder—all files and folders—into the myProductBK folder:

The code given below creates the myProductBK directory first, then copies all the files and folders into it if it hasn’t already been created:

@ECHO OFF

REM Remove/Delete Directory

MKDIR myProductBK

COPY Products\*.* myProductBK\

The batch file will generate the following error if the destination folder or directory doesn’t exist:

Here is the result of the batch file’s successful execution:

MOVE:

Using this command, files or directories can be moved from a source folder or directory to a destination folder or directory specified:

@ECHO OFF

REM MOVE Directory/Files

MKDIR myProdbackup

REM MOVE FILE(s)

MOVE Products\myProducts.txt myProdbackup\myProdbackup.txt

:: MOVE DIRECTORY with All Files

MOVE Products\*.* myProdbackup\

The Products folder becomes empty following the successful execution of the MOVE command since all the files and folders have been moved to the myProductbackup folder.

DEL:

Using this command, files can be deleted. The code below will delete all of the text files in the myProductBK folder.

Code Snippet:

@ECHO OFF

REM DEL command

DEL myProductBK\*.txt

REN:

We can rename files or folders using the REN (Rename) command. The first REN command changed the text file’s name, and the folder “Products” was renamed to “ProductsAug2012” by the second in the code given here:

@ECHO OFF

REM RENAME the Files/Folders

REN Products\productsbackup.txt products2012BK.txt

REN Products ProductsAug2012

ECHO Rename successful

DIR (Directory Listing):

It lists all of the files and directories in the active directory.

@ECHO OFF

:: DIR command is used to display the list of files

:: & folder in the directory with Date/Time

DIR

PING:

To check the connectivity to a remote host, employ this command.

@ECHO OFF

REM PING COMMAND

PING 127.0.0.1

CALL:

Another batch file can be called using the CALL command. The CALL command has a distinctive syntax:

CALL BATCH_FILE.bat [parameters]

The second line, the REM CALL tableof2.bat file, is a comment that tells you that the next line calls the tableof2.bat batch file. The third line, CALL tableof2.bat, calls the tableof2.bat batch file.

@ECHO OFF

REM CALL tableof2.bat file

CALL tableof2.bat

Here is the Output:

IF:

With the help of this command, we can check a condition and, if it’s true, run a block of code.

So, when we run the batch file code given below, the first line will turn off the echoing of commands. Then, the second line will check if the file myfile.txt exists. The fourth line will operate if the file is already present, and the statement “The file myfile.txt exists.” will be written. Otherwise, the sixth line will be executed, and the message “The file myfile.txt does not exist.” will be printed.

@ECHO OFF

:: Conditional Statement (IF ELSE)

IF EXIST myfile.txt (

ECHO The file myfile.txt exists.

) ELSE (

ECHO The file myfile.txt does not exist.

)

The output demonstrates that there are no myfile.txt in the working directory.

GOTO:

Use the GOTO command to navigate to any label in the batch file. If we want to skip or repeat a certain section of code, this can be beneficial. Therefore, the first line in the batch file will disable the command echoing when executed. The second line will then advance to the word START. The third line will then be executed and print “My Name is John.” The 8th line will be executed after the fourth line jumps to the label END and prints the message “My Father Name is Ibraham.”

@echo off

:: GOTO command jumps to the label

:START

ECHO My Name is John

GOTO END

ECHO I am 16 years old

:END

ECHO My Father Name is Ibraham

Conclusion

Batch files are a specific kind of text file that includes a list of commands the operating system will execute directly. Batch files are frequently used to automate file backup, application execution, and email sending.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content