Reprint Batch Bat command--get current drive letter and current directory and parent directory

Source: Internet
Author: User

Batch Bat command--get current drive letter and current directory and parent directory

Batch command gets current drive letter and current directory
%~D0 is the current drive letter
%cd% is the current directory
Can print test with echo%cd%

The following example is a command-line compilation of a program written by Visual Studio:
@echo off
Set b=%cd%//Save the current directory in parameter B with no spaces before and after the equals sign
CD Program Files
cd Microsoft Visual Studio
CD Common
CD msdev98
CD bin
Msdev "%B%\TEST.DSP"/make "Test-win32 Release"/rebuild//(VC6.0)
Devenv "%b%\tool utility\tool Utility.sln"/rebuild//(VS2010)
Note: If the path contains spaces, you should enclose the path plus the file name in double quotation marks.

Nonsense not much to say, look directly at examples:
@echo off
Echo Current drive letter:%~d0
Echo Current drive letter and path:%~dp0
Echo Current drive letter and path short name format:%~sdp0
echo Current Batch full path:%~f0
echo Current cmd default directory:%cd%

To fetch the top level directory of the current directory by batch processing
@echo off
If%cd%==%cd:~,3% echo the current directory is already the root of the%cd:~,1% disk! &goto End
Set "bd=%cd%"
Set "bbd=%cd%"
If "%bbd%" = = "%bd%" (Echo Upper level directory is:%cd:~,1% disk root directory!)
else call echo Upper level directory is: "%%bd:%bbd%\=%%"
: End

The above program I run and test, respectively, in the first level directory, the second level of the directory, the third level directory, the fourth level of the directory, are executed, the final result of the program is not correct, here only give you a thought, and all levels of the directory access method for reference only.

the difference between%cd% and%~dp0 in DOS batch processing
The difference between%cd% and%~dp0 in DOS batch processing, sometimes you need 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, also can be used in the command line, after expansion, is the drive letter: + The current directory, such as in the DOS window into the C:\dir directory, 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 of the batch file: + path. In the process of executing this batch file, the content after its expansion cannot be changed. For example, under the D disk there is a batch file, Path.bat, the contents are: [plain] @echo off echo This is the%%cd%%%cd% echo this is%%~dp0%~dp0 in c \ And e:\ execute it separately, the output is:


Reprint Batch Bat command--get current drive letter and current directory and parent directory

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: 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.