在批处理脚本中运行多个文件
作者:迹忆客
最近更新:2023/08/14
浏览次数:
大型脚本包含多个文件,因为它很容易维护代码。 使用较大的脚本时,您可能需要将它们划分为模块,以便更轻松地检测任何编码错误或问题。
但是,Batch 不具有面向对象的功能。 您可以为不同的模块创建不同的文件,并根据您的需要和要求从核心文件运行所有文件。
本文将展示如何从单个批处理脚本运行多个文件。 此外,我们将提供一些示例和解释,以使该主题更容易。
在批处理脚本中运行多个文件
为此,我们将使用名为 CALL 的内置命令。 从当前文件调用其他脚本文件的一般语法是:
CALL YourScript.bat
下面,我们分享了这个主题的一个大例子。 假设我们创建了下面共享的三个代码。
在 file1.bat 中,我们有以下代码:
ECHO This is from the first file
在 file2.bat 中,我们有以下代码:
ECHO This is from the second file
在 file3.bat 中,我们有以下代码:
ECHO This is from the third file
现在,在调用这些文件的核心文件中,我们有以下代码:
@echo off
ECHO This is the core file that calls all three files...
CALL "file1.bat"
CALL "file2.bat"
CALL "file3.bat"
现在,当您运行该文件时,您将得到以下输出:
This is the core file that calls all three files...
This is from the first file
This is from the second file
This is from the third file
请注意,如果您在这些文件目录之外运行 .bat 文件,则需要对主文件的代码进行一些编辑。 代码如下所示:
@echo off
ECHO This is the core file that calls all three files...
CALL "G:\BATCH\file1.bat"
CALL "G:\BATCH\file2.bat"
CALL "G:\BATCH\file3.bat"
您只需提供文件的完整目录。
请记住,我们此处讨论的命令仅适用于 Windows 命令提示符或 CMD 环境。