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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4663657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:57:53  来源:igfitidea点击:

Using Command Prompt To Call a Batch File in a Sub-Directory

windowscommand-linebatch-file

提问by KevinO

I'd like to use a batch file to call a different batch file that is in a sub directory. For instance, if my file system look as follows:

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

MainFolder
    main.bat
    FirstDirectory
    SecondDirectory
        foo.bat

MainFolder
    main.bat
    FirstDirectory
    SecondDirectory
        foo.bat

Then main.bat might look something like this:

然后 main.bat 可能看起来像这样:

echo on
REM This lines tells the user what this bat file is doing
call ant
call \SecondDirectory\foo.bat

I'm looking for a one line solution which I think does not exist. Unfortunately I don't always want to do this with a batch file and want to do it directly from the command line.

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

回答by Courtney Christensen

You can indeed call a batch file from another batch file using the callcommand. As @Blorgbeard states, the issues is the leading backslash \. Removing it indicates SecondDirectoryis relative to the current working directory.

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

call SecondDirectory\foo.bat

A word of caution: the callcommand essentially inserts called code in at run time. Be careful to avoid variable name collisions.

提醒一句:该call命令本质上是在运行时插入被调用的代码。小心避免变量名冲突