The difference between%cd% and%~dp0 in DOS batch processing

Source: Internet
Author: User

The difference between%cd% and%~dp0 in DOS batch processing

In DOS batches, it is sometimes necessary to know the current path.

In DOS, there are two environment variables that can be associated with the current path, one is%cd%, and the other is%~dp0.

The usage of the two variables and the content of the representation are different.

1.%cd% can be used in batch files or in the command line;

After expansion, is the drive letter: + current directory, such as in the DOS window into the C:\dir directory, www.2cto.com

Input: Echo%cd% is displayed as: C:\dir.

The contents of the%cd% can be changed to the execution path of the command or to the execution path of the batch file.

2.%~dp0 can only be used in a batch file, it is determined by the directory location of the batch file where it resides,

is the drive letter that contains the batch file: + path. In the process of executing this batch file,

The content after it is expanded cannot be changed. For example, under the D disk there is a batch file, Path.bat, the content is:

[Plain]

@echo off

echo This is%%cd%%%cd%

echo This is%%~dp0%~dp0

In C \ And e:\ execute it separately, the output is:


The difference between%cd% and%~dp0 in DOS batch processing

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.