% Cd % and % ~ in DOS batch processing ~ Dp0 differences

Source: Internet
Author: User
% Cd % and % ~ in Windows next batch processing ~ Dp0 can be used to represent the current directory, but they have different functions in different use cases. The following article details the % cd % and % ~ in DOS batch processing ~ The difference between dp0 and dp0 can be used for reference. % Cd % and % ~ in Windows next batch processing ~ Dp0 can be used to represent the current directory, but they have different functions in different use cases. The following article details the % cd % and % ~ in DOS batch processing ~ The difference between dp0 and dp0 can be used for reference.

Problem Description

Suppose we want to call and execute batch processing in batch processing a. batb.bat,b.batNeed to Knowb.batAnd executerun.exe, As follows:

// directory structure// c:// -a.bat// -program//  -b.bat//  -run.exe// a.batcall "%cd%\program\b.bat"// b.bat"%cd%\run.exe"

Then, can the task be successfully executed?run.exeWhat about it?

Problem analysis

%cd%And%~dp0Can be used to represent the current directory, but they have different functions in different use cases:

  • %cd%Represents the current working directory (variable );

  • %~dp0Indicates the full directory (the batch file's directory, fixed) where the current batch file is located ).

Let's take a look at the following example:

// directory structure// c:// -c.bat// -program//  -d.bat// c.batcall "%cd%\program\d.bat"// d.bat@echo offecho cd = %cd%echo dp0 = %~dp0

Run directlyd.bat, The result is

cd = C:\programdp0 = C:\program\

Run directlyc.bat, The result is

cd = C:\dp0 = C:\program\

The preceding results show that:

  1. Rund.batThe current working directory isd.batDirectory;

  2. Runc.batThe current working directory isc.batDirectory.d.batThe working directory is stillc.batDirectory.

Problem solving

Let's take a look at the problem mentioned in the problem description-can it be successfully executed?run.exeWhat about it?

The answer is: No. "%cd%\run.exe"Indicates"C:\run.exe", Not"C:\program\run.exe". So how can we change it?

There are two solutions:

// plan A// change the current working directory// a.batcd "%~dp0"call "%cd%\program\b.bat"// b.batcd "%~dp0""%cd%\run.exe"// plan B// using %~dp0 directly// a.batcall "%~dp0program\b.bat"// b.bat"%~dp0run.exe"

Problem extension

In the above solution, plan A solves this problem by changing the current directory. another problem exists here. let's take A look at the example below:

// directory structure// c:// -program//  -f.bat// d:// -e.bat// plan A// change the current working directory// e.batcd "%~dp0"call "c:\program\f.bat"// f.batcd "%~dp0""%cd%\run.exe"

Nowe.batAndf.batNot in the same drive letter.e.batSwitch the current working directoryf.batYou cannot use cd directly. you must use:

cd /d "%~dp0"

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

Problem summary

Let's reiterate%~dp0And%cd%, %cd%And%~dp0Can be used to represent the current directory, but they have different functions in different use cases:

  • %cd%Represents the current working directory (variable );

  • %~dp0Indicates the full directory (the batch file's directory, fixed) where the current batch file is located ).

From the current usage, try to use%~dp0, Not recommended%cd%, Except for other requirements.

More % cd % and % ~ in DOS batch processing ~ For details about the differences between dp0 and dp0, please follow the PHP Chinese network!

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.