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
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 file name in double quotation marks.
Source: http://blog.csdn.net/ylmmee/article/details/6735907
===============================================================

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%
Pause
Http://www.2cto.com/kf/201104/88450.html
===============================================================

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
Cd..
Set "bd=%cd%"
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
Pause

P.S.
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.
Source: HTTP://ZHIDAO.BAIDU.COM/LINK?URL=_3UFT92HVTRXF0VINIX4YVUEVQHQ448_97GLGBZAXN-8SWIOC3RKJGGGFPSXPI1H8UUKCV5YE0JNNA3CLQMHJQ

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