Batch process gets current path%cd%_dos

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 representative is generally different.

1.%cd% can be used in a batch file or in a command line, and after expansion, the drive letter: + current directory, such as:

Under the C:/dir,

BAT code echo%cd%

Echo%cd%

Shown as,

BAT Code C:/dir

C:/dir

%cd% 's time content can be changed, such as the CD command can change its content.

2. The%~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, Dirshow.bat:

BAT code @echo off the echo this is%%cd%%%cd% the echo this is%%~dp0%~dp0

@echo off the echo this is
%%cd%%%cd% The echo this is
%%~dp0%~dp0

In c:/ To execute it, the output is:

BAT code c:/>d:/dirshow.bat This are%cd% c:/this is%~dp0 d:/

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.