Updated April 10, 2023
Introduction to Batch Scripting Commands
In laymen terms, we can simply say that the batch file is a simple text file with .bat extension, which is generally written by using the text editor like sublime, notepad++ and UltraEdit, etc.
.bat extension shows that it is a batch file which will run over the command prompt, this file contains several commands in particular sequence to perform or automating a task using command prompt. we can run the batch file by double click on it. and it will start executing the command on command prompt one by one in the command prompt.
What is Batch Scripting?
A batch script is a file which contains a sequence of command needs to be executed on windows command prompt, batch script is always referred for executing a series of command over the windows command prompt, in Windows operating system it is known as batch script, whereas in Linux it is known as a shell script.
When we execute the batch script file over the command prompt, all the command get executed one by one by following the sequence written in the batch file. each command is supplied as a command parameter for command prompt, the output of the batch file can be control or logged into a file, which helps us to keep record and identifying the error while executing the command.
List of Batch Scripting Commands
Following are the list of Batch Scripting Commands:
- Echo: It shows the output of the current command which is being executed, by default echo is on for all the batch script fils, it means when the sequence of command will be executed one by one in the on the command prompt, the corresponding command output will be displayed over the command prompt, if we want we can turn it off by writing @echo of.
using @ before the echo off / echo on command prevents itself from displaying itself as echo as in output. - Rem: rem is used to comment on the line (command) in the batch file, the line starting with rem is ignored by the command prompt. instead of using rem we can use:: (double colon) operating to make the line commented.
- Call:call command is used to call another batch file from inside a batch file, the batch file which call another batch file knows as caller and the file which is called known as callee, call command always return to the caller batch file even if the error exists in the callee batch file, call command always share the variable reference among the caller/callee batch files, if there is two batch file which is dependent on each other in this case call command is used to call dependent batch file from one batch file, call always execute the file in the same window and share variable context among the commands.
- Start: start command is used to call another batch file from inside a batch file, the main difference is, start command always open a new window to execute a command file and it never share the variable context among the file, the file which is being called from inside a batch file will run as a separate process and open the new command console to run the series of command.
- GOTO: goto command jump to a specific label of batch file, goto command is used with label, which indicates the specific label name in batch file, label always start with : (colon), name of the label is defined just after the goto (on which the goto command moves the control) execute the command and it return from where it was jumped to that label.
- |: When we need to use the output of the one command as input of the second command “|” operator is used between the command, it refers to the output of the one command as input of the second command.
- &: It separates the command into multiple lines.
- &&: It executes the current command if and only if the previous command is successfully run or its error level is zero.
- >: It used to write the command output in the file.
- >>: It appends the output of the current command which is being executed into the file.
Certain Important Batch Commands
Here we discuss some important Batch Scripting Commands:
Command Name | Uses |
Taskill -f /pid 2035 | Killing the process of given process id |
Ipconfig /all | Display system IP information |
attrib | Set or display the file attribute |
cd | Change the directory |
Shutdown | Shutdown the computer |
Sort | To sort the file content |
Systeminfo | Display all the system configuration |
Tasklist | Display the all running task list |
Time | Set or display the system time |
Title | Set the batch script title |
Type | Display the file content on the console |
Ver | Display the windows version |
Copy | Copy a single file from or to the directory |
Xcopy | It copies the file and its subsidiaries |
Ren | Displaying remaining file and directory |
Assoc | display association of the file extension |
Examples
Below are mentioned the examples of Batch Scripting Commands:
Example #1
@echo off
echo 'Hello world from educba'
Let’s save this batch file as test.bat and run this file by double-clicking on it, the following output you will get.
Output:
Hello world from educba
Example #2
@echo off
echo 'System ip information'
ipconfig
rem ipconfig /all
Lets save this batch file as test.bat and run this file by double-clicking on it, this will print the system IP information of the system on which the batch file is running, the line which starts with rem is ignored hence the information related to config /all command will not be printed., we can also use:: (double colon) operator instead of ram to make the config /all line as comment.
Comments are very useful to write a description for the command in a large batch file which contains any number of command and reference of another batch file; it makes the batch file easy to understand and modifiable.
Example #3
@echo off
echo 'System port information'
netstat -ano | findstr '8080'
This batch script prints the process information which is running on or port number 8080.
Example #4
@echo off
echo 'run java jar file with name demo'
java -jar target/demo.jar > log.txt
This will run java demo jar file, just navigate to your java maven base directory and run this batch file.
Example #5
@echo off
echo 'Trace route for given host name'
@echo off
title trace the root to the host
Set /p host_name= enter hostname or ip for which you want to trace the route:
if defined host_name (
powershell -Command tracert -d %Host% >>tracert_result.txt
)
This batch file will take the hostname or IP address of the destination and will append the traceroute result of that destination host into tracert_result.txt file, this file will run on Windows Powershell.
Example #6
@echo off
echo ''deploy project on server'
call package.bat
call integration-test.bat
call deploy.bat
Example #7
@echo off
echo 'start the project deployment assembly'
cd "\package\demo\"
start package.bat
cd "\test\demo\"
start test.bat
cd "\deploy\demo\"
start deploy.bat
This batch file will run each of the files which belong to package, test, and deployment respectively; as a separate manageable process and will open a new console to log a command output for each of the batch file.
Conclusion
The batch file was a very popular solution to executing a series of command one by one when DOS used to be a very popular operating system, Nowadays also batch script plays a very important role in various tasks such as to automate the configuration of the windows server or to schedule a task.
Recommended Articles
We hope that this EDUCBA information on “Batch Scripting Commands” was beneficial to you. You can view EDUCBA’s recommended articles for more information.