在批处理脚本中运行带有参数的批处理文件

MD Aminul Islam 2022年6月21日
在批处理脚本中运行带有参数的批处理文件

有时我们需要使用必要的参数从当前 Batch 脚本运行一个新的 Batch 脚本。本文将讨论如何从批处理文件中运行具有多个参数的另一个批处理文件。

在批处理脚本中运行带有参数的批处理文件

要运行带有参数的批处理脚本,我们必须遵循这种通用格式,YourScript.bat Parameter_1 Parameter_2 Parameter_3。你将在参考脚本(如 %1 %2 ... %n)上以数字序列获得参数。

让我们看一个带有解释的示例,以使其更容易。

下面是我们的参考脚本。脚本内的代码在下面共享。

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

上面的代码将打印参数。引用上述代码的代码如下:

TestScript.bat 300 250 800

我们将参考脚本重命名为 TestScript.bat。当你点击 Enter 时,你将获得如下输出。

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

让我们来看一个复杂的例子。现在我们将创建一个引用脚本来获取参数,执行添加操作,并向我们展示结果。

我们上述参考脚本代码的修改版本将如下所示。

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

以与之前相同的方式调用脚本后,你将获得如下输出。

TestScript.bat 300 250 800

输出:

The first parameter is 300
The second parameter is 250
The third parameter is 800
The result is 1350
作者: MD Aminul Islam
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

相关文章 - Batch File