call batch file from another passing parameters
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 ano开发者_StackOverflow中文版ther and pass the parameters values to the above batch file, and the user wont be asked for input. How can I achieve this?
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.
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
caller.bat /.cmd
@echo off
cls
set vara="Hello There"
set varb=67890
echo Variables set in caller.bat:
echo %vara%
echo %varb%
echo Calling passTo.bat
call passTo.bat %vara% %varb%
passTo.bat /.cmd
@echo off
echo.
echo Inside passTo.bat
set vara=%1
set varb=%2
echo vara: %vara%
echo varb: %varb%
Calling passTo.bat on its own from dos or command windows or any other application/shortcut
call passTo.bat PARAM1 PARAM2
Using the set Variables from caller.bat inside another batch
@echo off
echo.
echo Inside another.bat
echo -- You can start another batch here that sets the environment variables
set varOne=%vara% <- This gets the Environments Variable set in the nested batch bat using SET
set varTwo=%varb%
echo vara: %varOne%
echo Is the same as
echo %vara%
echo varb: %varTwo%
echo is again the same as
echo %varb$
You might have to use SETX
精彩评论