Directory navigation in command prompts

2019-08-07 03:05发布

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"

1条回答
Deceive 欺骗
2楼-- · 2019-08-07 04:01

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).

查看更多
登录 后发表回答