Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between% cd% and% ~ dp0 in DOS batches

Shulou Source: shulou.com Published: 2022-06-03 16:12:37 11月14日 Update

This article mainly explains the "DOS batch processing% cd% and% ~ dp0 what is the difference", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "what is the difference between% cd% and% ~ dp0 in DOS batch processing"!

In batch processing of DOS, 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 usage and representation of these two variables are different.

1.% cd% can be used in batch files or on the command line; when expanded, it is the drive letter: + current directory, such as entering the c:\ dir directory in the dos window

Enter: echo% cd%, it will be displayed as: C:\ dir.

The content of% cd% can be changed, which is the execution path of the command or the execution path of the batch file.

2.%~dp0 can only be used in batch files, it is determined by the directory location of the batch file in which it is located, and it is the drive letter: + path where the batch file is located. During the execution of this batch file, its expanded contents cannot be changed. For example, under disk D, there is a batch file, path.bat, which contains:

The code is as follows:

@ echo off

Echo this is cd%% cd%

Echo this is% ~ dp0% ~ dp0

Execute it under C:\ and E:\ respectively, and the output is:

Thank you for your reading, these are the contents of "what is the difference between% cd% and% ~ dp0 in DOS batches?" after the study of this article, I believe you have a deeper understanding of what the difference between% cd% and% ~ dp0 in DOS batches is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Content file path directory learning two variables commands location different under code representative location that is ideas situations articles are made up of more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Apple Shulou Technology Xiaomi