How to Run a Batch File With Parameters in Batch Script

MD Aminul Islam Feb 02, 2024
How to Run a Batch File With Parameters in Batch Script

Sometimes we need to run a new Batch script from the current Batch script with the necessary parameters. This article will discuss how to run another Batch file with multiple parameters from a Batch file.

Run a Batch File With Parameters in Batch Script

To run a Batch script with parameters, we must follow this general format, YourScript.bat Parameter_1 Parameter_2 Parameter_3. You will gain the parameter in a numeric sequence on the reference script like %1 %2 ... %n.

Let’s see an example with an explanation to make it easier.

Below is our reference script. The codes inside the script are shared below.

@echo off
ECHO The first parameter is %1
ECHO The second parameter is %2
ECHO The third parameter is %3

The above code will print the parameters. The code that will reference the code above will be like:

TestScript.bat 300 250 800

We renamed our reference script TestScript.bat. When you hit Enter, you will get an output like the below.

The first parameter is 300
The second parameter is 250
The third parameter is 800

Let’s go with a complex example. Now we will create a reference script to take the parameter, perform an add operation, and show us the result.

The modified version of our above reference script code will look like this.

@echo off
ECHO The first parameter is %1
ECHO The second parameter is %2
ECHO The third parameter is %3

After calling the script the same way as before, you will get an output like the below.

TestScript.bat 300 250 800

Output:

The first parameter is 300
The second parameter is 250
The third parameter is 800
The result is 1350
MD Aminul Islam avatar MD Aminul Islam avatar

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

Related Article - Batch File