从另一个传递参数调用批处理文件

发布于 2024-11-05 04:39:23 字数 268 浏览 1 评论 0原文

我有以下批处理文件,运行时要求用户输入。这很好用。

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

我现在希望能够从另一个批处理文件调用此批处理文件并将参数值传递到上面的批处理文件,并且不会要求用户输入。我怎样才能实现这个目标?

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 won't be asked for input. How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空袭的梦i 2024-11-12 04:39:23

我相信你想要这样的东西?

@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 不带参数运行:

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

Test.bat 带参数运行:

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

替代方案,使用环境变量而不是命令行参数(另请参阅 ppumkin 的回答):

@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%

只需设置环境变量 globalparam1globalparam2 在您的环境或您调用的批处理文件中抑制提示:

Test.bat run withoutsettingenvironmentvariables:

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

Test.batrunwithsettingenvironmentvariables:

>>set globalparam1=foo

>>set globalparam2=bar

>>test
param1=foo
param2=bar

注意:设置环境变量也可以在例如调用批处理脚本。

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.

仅一夜美梦 2024-11-12 04:39:23

main.cmd 中:

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

caller.cmd 中:

call main.cmd hello world

输出:

hello - world

批处理脚本参数参考

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

清泪尽 2024-11-12 04:39:23

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%

从 dos 或命令窗口或任何其他应用程序/快捷方式单独调用 passTo.bat

call passTo.bat PARAM1 PARAM2

在另一个批处理中使用 caller.bat 中的设置变量

@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$

您可能必须使用 SETX

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文