使用命令提示符调用子目录中的批处理文件

Kev*_*inO 5 windows command-line batch-file

我想使用批处理文件来调用子目录中的其他批处理文件.例如,如果我的文件系统如下所示:

MainFolder
    main.bat
    FirstDirectory
    SecondDirectory
        foo.bat

那么main.bat可能看起来像这样:

echo on
REM This lines tells the user what this bat file is doing
call ant
call \SecondDirectory\foo.bat
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种我认为不存在的单线解决方案.不幸的是,我并不总是希望用批处理文件执行此操作,并希望直接从命令行执行此操作.

Cou*_*sen 12

您确实可以使用该call命令从另一个批处理文件中调用批处理文件.正如@Blorgbeard所说,问题是领先的反斜杠\.删除它表示SecondDirectory相对于当前工作目录.

call SecondDirectory\foo.bat
Run Code Online (Sandbox Code Playgroud)

需要注意的是:该call命令实际上是在运行时插入调用的代码.小心避免变量名称冲突.

  • 使用调用".\ folder\file.bat"也可以工作(.是当前目录和引用路径通常是个好主意) (6认同)