The difference between DOS batch CD and ~DP0

Source: Internet
Author: User

Problem description

Suppose we want to call the execution batch in the batch A.bat B.bat,b.bat need to know the B.bat's current location and execute Run.exe as follows:

1 2 3 4 5 6 7 8 9 10 Directory structure//C://A. BAT//-program//b. bat//-run. exe//A.bat call "%cd%\program\b.bat"//B.bat "%cd%\run.exe"

Now, then, can we successfully execute Run.exe?

Problem analysis

Both%cd% and%~dp0 can be used to represent the current directory, but they are not the same in different usage scenarios:%cd% represents the current working directory (present working directory,variable); DP0 represents the full directory of the current batch file (the batch file ' s directory,fixed).

Let's take a look at the following example:

1 2 3 4 5 6 7 8 9 10 11 Directory structure//C://-C. BAT//-program/D. BAT//C.bat call "%cd%\program\d.bat"//D.bat @ echo off Ech o cd =% cd% echo dp0 =%~dp0

Run D.bat directly, the result is

1 2 cd = C:\program Dp0 = C:\program\

Run C.bat directly, the result is

1 2 cd = C:\ Dp0 = C:\program\

As you can see from the results above: When executing D.bat, the current working directory is the directory of D.bat, and the current working directory is C.bat directory when the C.bat is executed, even after the d.bat is invoked, the working directory is still the C.bat directory.

Problem solving

Let's take a look at the problems mentioned in the description of the problem--can we successfully execute Run.exe?

The answer is: No. "%cd%\run.exe" means "C:\run.exe", not "C:\program\run.exe". So how to change it.

There are two types of scenarios:

1 2 3 4 5 6 7 8 9 10 11 12 13-14 Plan A//change the current working directory//A.bat CD "%~dp0" Call "%cd%\program\b.bat"//B.bat CD "%~dp0" "%cd%\ Run.exe "//plan B//using%~DP0 directly//A.bat call"%~dp0program\b.bat "//B.bat"%~dp0run.exe "

Problem extension

In the above solution, plan a solves the problem by changing the current directory, and there is another problem, let's look at the following example:

1 2 3 4 5 6 7 8 9 10 11 12 13-14 Directory structure//C://-program//F. BAT//D://E. BAT//Plan A//change to current working directory// E.bat CD "%~dp0" Call "C:\program\f.bat"//F.bat CD "%~dp0" "%cd%\run.exe"

Now that E.bat and F.bat are not in the same letter, switching from E.bat current working directory to f.bat directly using the CD is not possible and must be used:

1 cd/d "%~dp0"

This place is easy to neglect, remember not to make mistakes.

Summary of issues

Let's reiterate the difference between%~dp0 and%cd%,%cd% and%~dp0 can be used to represent the current directory, but they are not the same in different usage scenarios:%cd% represents the current working directory (present working directory,variable) %~DP0 represents the full directory of the current batch file (the batch file ' s directory,fixed).

From the current use of our situation, try to use%~DP0, do not recommend the use of%cd%, except for other requirements.

Summary

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.