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:
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
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 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
If%cd%==%cd:~,3% echo the current directory is already the root of the%cd:~,1% disk! &goto End
If "%bbd%" = = "%bd%" (Echo Upper level directory is:%cd:~,1% disk root directory!)
else call echo Upper level directory is: "%%bd:%bbd%\=%%"
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, www.2cto.com 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