Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call batch file from another passing parameters

Tags:

batch-file

cmd

I have the following batch file that when run asks the user for input. This works fine.

@REM Sample batch file
SET PARAM1=""
SET PARAM2=""
SET /P PARAM1=Enter param1: %=%
SET /P PARAM2=Enter param2: %=%
@REM Process the params

I want to be able now to call this batch file from another and pass the parameters values to the above batch file, and the user wont be asked for input. How can I achieve this?

like image 913
amateur Avatar asked May 04 '11 12:05

amateur


People also ask

How do I pass parameters to a batch file?

In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

How do you call a batch file with parameters from a PowerShell script?

To run a batch file ( cmd.exe shell script) from a PowerShell prompt, just type the batch file's name, followed by its parameters, and press Enter . Remember: PowerShell is a shell, which means it runs command you type, just like cmd.exe does. For example: D:\load.

What is %% A in batch script?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What does @echo off do?

The ECHO-ON and ECHO-OFF commands are used to enable and disable the echoing, or displaying on the screen, of characters entered at the keyboard. If echoing is disabled, input will not appear on the terminal screen as it is typed. By default, echoing is enabled.


2 Answers

I believe you want something like this?

@echo off

:: Fetch param1
set "param1=%~1"
goto :param1Check
:param1Prompt
set /p "param1=Enter parameter 1: "
:param1Check
if "%param1%"=="" goto :param1Prompt

:: Fetch param2    
set "param2=%~2"
goto :param2Check
:param2Prompt
set /p "param2=Enter parameter 2: "
:param2Check
if "%param2%"=="" goto :param2Prompt

:: Process the params
echo param1=%param1%
echo param2=%param2%

Test.bat run without arguments:

>>test.bat
Enter parameter 1: foo
Enter parameter 2: bar
param1=foo
param2=bar

Test.bat run with arguments:

>>test.bat foo bar
param1=foo
param2=bar

Alternative, using environment variables instead of command line arguments (see also ppumkin's answer):

@echo off  

:: Fetch param1  
**set "param1=%globalparam1%"**  
goto :param1Check  
:param1Prompt  
set /p "param1=Enter parameter 1: "  
:param1Check  
if "%param1%"=="" goto :param1Prompt  

:: Fetch param2    
**set "param2=%globalparam2%"**  
goto :param2Check  
:param2Prompt  
set /p "param2=Enter parameter 2: "  
:param2Check  
if "%param2%"=="" goto :param2Prompt  

:: Process the params  
echo param1=%param1%  
echo param2=%param2%

Just set the environment variables globalparam1 and globalparam2 in your environment or your calling batch file to suppress the prompting:

Test.bat run without setting environment variables:

>>test.bat
Enter parameter 1: foo
Enter parameter 2: bar
param1=foo
param2=bar

Test.bat run with setting environment variables:

>>set globalparam1=foo

>>set globalparam2=bar

>>test
param1=foo
param2=bar

Note: setting the environment variables can also be done in e.g. a calling batch script.

like image 67
mousio Avatar answered Sep 22 '22 13:09

mousio


In main.cmd:

set param1=%~1
set param2=%~2
echo %param1% - %param2%

In caller.cmd:

call main.cmd hello world

Output:

hello - world

Reference for batch script parameters

like image 45
Benoit Avatar answered Sep 23 '22 13:09

Benoit