命令提示符中的目录导航
我正在尝试从另一个文件夹中的批处理文件运行一个文件夹中存在的批处理文件:
父文件夹 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您是否真的需要导航到所需的文件夹(即将其设置为当前文件夹),或者您只是需要一种使用相对路径表示法来调用该文件夹中的批处理脚本的方法。 导航,从我对这个词的理解来看,意味着前者,但你的最后一句话似乎表明你需要后者。
一、答案:
接下来,是什么意思。
%~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:
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 theBatch2.bat
's folder, and the grand-parent isBig
. Once we are pointing toBig
, we can address the files/folders in it, in this case it'sBatchFolder
that we need, and eventually we can put the name ofBatch1.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).