命令提示符中的目录导航

发布于 2024-10-18 05:11:48 字数 335 浏览 4 评论 0原文

我正在尝试从另一个文件夹中的批处理文件运行一个文件夹中存在的批处理文件:

父文件夹 Big 包含 2 个文件夹 BatchFolder 和 Medium。 BatchFolder 包含一个名为 Batch1 的批处理文件。 Medium 包含另一个名为 Small 的文件夹。 Small 包含一个名为 Batch2 的批处理文件,需要运行 Batch1。 命令提示符是从 Batch2 的位置运行的。

因此,如何将文件夹向上导航到“大”,然后导航到 BatchFolder?

我一直在尝试实现这一目标,但没有成功,例如 Bacth2 包含以下“call ../BatchFolder/Batch1.bat”

I'm trying to run a batch file that exists in one folder, from a batch file in another folder:

Parent folder Big containes 2 folders BatchFolder and Medium.
BatchFolder contains a batch file called Batch1.
Medium contains another folder called Small.
Small contains a batch file called Batch2 that needs to run Batch1.
The command prompt is run from the location of Batch2

Therefor, how do I navigate up the folders To Big, and then navigate into the BatchFolder?

I've been trying alsorts to achieve this with no success, such as Bacth2 containing the following "call ../BatchFolder/Batch1.bat"

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

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

发布评论

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

评论(1

想念有你 2024-10-25 05:11:48

我不确定您是否真的需要导航到所需的文件夹(即将其设置为当前文件夹),或者您只是需要一种使用相对路径表示法来调用该文件夹中的批处理脚本的方法。 导航,从我对这个词的理解来看,意味着前者,但你的最后一句话似乎表明你需要后者。

一、答案:

call %~dp0%..\..\BatchFolder\Batch1.bat

接下来,是什么意思。 %~dp0%%0 的变体:后者是此批处理文件(即 Batch2.bat)的完整路径,包括文件名,前者是该批处理文件夹的完整路径(包括尾随的 \)。

.. 指向直接父文件夹。重复两次是因为我们需要访问 Batch2.bat 文件夹的“祖父母”,而祖父母是 Big。一旦我们指向 Big,我们就可以寻址其中的文件/文件夹,在本例中,它是我们需要的 BatchFolder,最终我们可以将 的名称放入>Batch1.bat

无论当前文件夹是什么,这都有效。也就是说,如果我不清楚这一点,只需调用批处理文件,您还没有更改当前文件夹。为此,您必须使用 CD 命令。这将是导航(但我仍然愿意接受纠正我对这个术语的理解)。

I'm not sure whether you really need to navigate to the required folder (i.e. set it as the current one) or you simply need a way to call the batch script in that folder using a relative path notation. Navigating, from how I understand the term, means the former, but your last sentence seem to show that you need the latter.

First, the answer:

call %~dp0%..\..\BatchFolder\Batch1.bat

Next, what it means. %~dp0% is a variation of %0: the latter is the full path to this batch file (i.e. Batch2.bat) including the file name, and the former is the full path to this batch's folder (including the trailing \).

.. points to the immediate parent folder. It is repeated twice because we need to access the 'grand-parent' of the Batch2.bat's folder, and the grand-parent is Big. Once we are pointing to Big, we can address the files/folders in it, in this case it's BatchFolder that we need, and eventually we can put the name of Batch1.bat.

This works regardless of what the current folder is. That is, in case I wasn't clear on that head, by simply calling a batch file you are not yet changing the current folder. You would have to use the CD command for that. That would be navigating (but I'm still open to being corrected as to my understanding of this term).

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