The similarities and differences of%cd% and%~dp0 in DOS batch processing _dos/bat

Source: Internet
Author: User
In DOS batches, it is sometimes necessary to know the current path. In DOS, there are two environment variables that can relate to the current path, one is%cd%, and the other is%~dp0.

The use of these two variables and the content of the representation are different.

1.%cd% can be used in a batch file or in a command line; When expanded, the drive letter: + current directory, such as in the DOS window into the C:\dir directory,

Input: Echo%cd%, which is displayed as: C:\dir.
The contents of the%cd% can be changed to the execution path of the command or the execution path of the batch file.

2.%~dp0 can only be used in a batch file, which is determined by the directory location of the batch file in which it resides, and is the disk character of the batch file: + path. In the process of executing this batch file, the content that it expands can not be changed. For example, under D disk, there is a batch file, Path.bat, which reads:
Copy Code code as follows:

@echo off
echo This is%%cd%%%cd%
echo This is%%~dp0%~dp0


In C:\ and E:\ to execute it separately, the output is:

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.