Batch BAT command gets the code for the current letter and current directory and ancestor directory _dos/bat

Source: Internet
Author: User

Batch command gets the current letter and current directory

%~D0 is the current disk character
%cd% is the current directory

You can print the test with Echo%cd%

The following example is the command line that compiles a program written by Visual Studio:

Copy Code code as follows:

@echo off
Set b=%cd%//Save current directory to parameter B, no spaces before and after equals sign
C:
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)
Pause

Note: If the path contains spaces, you should enclose the path plus the filename in double quotes.

Don't say much nonsense, just look at the example:

Copy Code code as follows:

@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%
Pause

To get the top level of the current directory through batch processing

Copy Code code as follows:

@echo off
The current directory of If%cd%==%cd:~,3% echo is already the root directory of the%cd:~,1% disk! &goto End
Cd..
Set "bd=%cd%"
Cd..
Set "bbd=%cd%"
If "%bbd%" = = "%bd%" (Echo Level directory is:%cd:~,1% disk root directory!)
else call echo Upper level directory is: "%%bd:%bbd%\=%%"
: End
Pause

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

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.