% Cd % and % ~ in DOS batch processing ~ Difference of dp0

Source: Internet
Author: User

Looking at the differences introduced on the internet, writing is complicated, but it is actually very simple:

% Cd % can be used in both the batch processing and command windows. Used to print the current working path.

% ~ Dp0 % can only be used in batch processing to obtain the path of the current batch processing file.

 

Try it out:

@echo off     echo this is %%cd%%  %cd%     echo this is %%~dp0 %~dp0   echo switch to another folder:cd d:\Program Files (x86)echo this is %%cd%%  %cd%     echo this is %%~dp0 %~dp0 pause

The result shows that % Cd % can be switched to another directory through CD in the script, and the print path is dynamic.

 

In fact, it should be noted that most of the scenarios are different, CD to different working paths, is a common dos operation. % Cd % is set to get the current working path.

 

 

However, another common requirement is to obtain the path of the current BAT file, so this path needs to use % ~ Dp0 %. The trouble is that bat1 calls bat2 again. Which path does it obtain?

Try it and you will know:

T1.bat:

@echo offecho this is t1.batecho %%~dp0%% get path: %~dp0% cd D:\learn\Bat\codescall t2.batpause

 

T2.bat:

echo this is t2.batecho %%~dp0%% get path : %~dp0%pause

 

T1 is in the root directory of disk D, and t2.bat is in disk D: \ learn \ BAT \ codes. The execution result of t1.bat is:

It can be seen that the path of the file where the current command is located prevails.

 

Note that the variable name itself should be ideally added to the batch, and a % should be added to the outer layer.

% Cd % and % ~ in DOS batch processing ~ Difference of dp0

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.