Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a batch bat command to get the current drive letter and the code for the current directory and parent directory

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to write batch bat command to get the current drive letter and the current directory and superior directory code", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of small series slowly in-depth, together to study and learn "how to write batch bat command to get the current drive letter and the current directory and superior directory code" bar!

Batch command Get current drive letter and current directory

Current drive letter: %~d0

Current path: %cd%

Current execution command line: %0

Current bat file path: %~dp0

Short path of current bat file: %~sdp0

test

echo Current drive letter: %~d0echo Current path: %cd%echo Current execution command line: cho Current bat file path: %~dp0echo Current bat file short path: %~sdp0

The effect is shown below

You can print tests with echo %cd%

Windows shortcuts cannot use relative paths. You can use batch files to get relative paths.

set pa=%cd% Assign current path to pa

echo %pa% show pa variable

The following examples are command-line compilations of programs written in Visual Studio:

@echo offset b=%cd% //Save the current directory to parameter b without spaces before and after the equal sign C:cd program filescd microsoft visual studio cd common cd msdev98cd binmsdev "%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 quotes.

Without further ado, look directly at the example:

@echo offecho Current drive: %~d0echo Current drive and path: %~dp0echo Short file name format of current drive and path: %~sdp0echo Current batch full path: %~f0echo Current CMD default directory: %cd%pause

Get the directory one level above the current directory through batch processing

@echo offif %cd%==%cd:~,3% echo The current directory is already the root directory of the %cd:~,1% disk!& goto endcd.. set "bd=%cd%"cd.. set "bbd=%cd%"if "%bbd%"=="%bd%" else call echo next level directory is: "%%bd:%bbd%\=%%":endpause

P.S.

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

A bat used by Xiaobian before, convenient to delete the instructions in the folder and copy your own instructions

@echo offsetlocal EnableDelayedExpansiondel jb51.txt /sdel Description.htm /scopy F:\Edit\jb51tools\jb_down\book\ %cd%mkdir jb51.netdel%0| move *.* jb51.net Thank you for reading, the above is "how to write batch bat command to obtain the current drive letter and the current directory and superior directory code" content, after learning this article, I believe we have a deeper understanding of how to write batch bat command to obtain the current drive letter and the current directory and superior directory code, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report