How to Run CMD Commands in Batch Script
Are you looking for a way to run Windows Command Prompt commands using a Batch Script? Then you can follow this article.
This article will discuss ways to enable you to run Command Prompt commands from your Batch Script. We will discuss the technique step by step.
Also, we will see necessary examples and explanations to make the topic easier to understand.
Steps to Run CMD Commands Using Batch Script
Batch is a specially developed scripting language for Windows Command Prompt environment. It can also run Command Prompt commands.
You can have a sequence of CMD commands to perform a specific task. You only need to copy all these commands, paste them into a Batch file (.bat
), and run the script.
In this way, you need not execute all the commands individually. Let’s see the steps to combine all the Command Prompt commands in one Batch Script.
Organize Your Commands
This is our first step and the most important step. In this step, we will organize our commands in sequence.
This process must be done carefully, as any incorrect sequencing of commands may cause the process to fail. For example, when writing a command to run a file, you first need to change the directory where the file is using the CD
command and then mention the file name to run the file.
So, you need to follow this sequence when writing a Batch script for running a specific file from your directory.
Put Your Commands in the Batch File
When you finish the first step or organize your commands, it’s time to put them together on your Batch script. To do this, you must copy all the commands and paste them into a file.
Then, save the file with the .bat
extension. Here is a similar example:
C:\
CD C:\Program files\IIS Express
START iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
START http://localhost:8088/default.aspx
PAUSE
Run Your Batch Script
This is the last step. In this step, you only need to run the script.
You can follow two ways here. The first one is very easy; double-click on the .bat
file.
The second one is to open the Command Prompt and run the .bat
file. You can follow the below example to run your .bat
file,
"G:\Batch_code\BatchTest.bat"
You only need to put the exact directory of your .bat
file into a double quote.
Conclusion
This article is very helpful when you are executing the same commands again and again. You need not run each command one by one; you only need to run your .bat
file.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn