China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 00:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to get the current drive letter and save it in a text file? View 7,019 Replies 18
Original Poster Posted 2006-11-18 06:44 ·  中国 四川 成都 电信
初级用户
Credits 105
Posts 44
Joined 2006-10-05 13:57
19-year member
UID 64618
Status Offline
For example, to run a software on a removable disk, but the drive letter will be different on different machines!
Floor 2 Posted 2006-11-18 06:52 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 3 Posted 2006-11-18 06:55 ·  中国 四川 成都 电信
初级用户
Credits 105
Posts 44
Joined 2006-10-05 13:57
19-year member
UID 64618
Status Offline
It still doesn't seem to work!
Floor 4 Posted 2006-11-18 07:07 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Try again: (Write the following content into a BAT file to execute)
set a=%~0
echo %a:~0,2%>temp.txt
Or use the following code:
echo %~d0>temp.txt
Floor 5 Posted 2006-11-18 07:09 ·  中国 四川 成都 电信
初级用户
Credits 105
Posts 44
Joined 2006-10-05 13:57
19-year member
UID 64618
Status Offline
Thanks, moderator. It's already okay.
Floor 6 Posted 2006-11-18 07:24 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Regarding the acquisition of the current drive letter, there are generally the following three schemes:

1. cd command:

@echo off
echo Current path is: %cd%
echo Current drive letter is: %cd:~0,1%
pause


2. for statement extension:

@echo off
for %%i in (.) do set var=%%~di
echo Current drive letter is: %var:~0,1%
pause


3. Parameter %~0 scheme:

@echo off
set a=%~0
echo Current drive letter is: %a:~0,1%
pause


Of course, there are other schemes. Generally, they all use the for statement to extract the characters at the specific position of the return value, and without exception, they all need to be implemented by intercepting characters (The statement in blue is not completely accurate. For specific situations, please refer to the example in 11F).

[ Last edited by namejm on 2006-11-24 at 08:58 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +3 2006-11-26 00:02
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-11-18 07:38 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 8 Posted 2006-11-18 08:07 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by Must Love at 2006-11-17 18:38:
%~0 doesn't get the quoted current path啊

  Hehe, sorry, I made a mistake, I was missing a ~ symbol.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2006-11-23 07:10
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline

Of course, there are other solutions. Generally, for statements are used to extract characters at specific positions in the return value, and without exception, they all need to be implemented by intercepting characters.


It seems there can be an exception. I don't know if this works?


@echo off
set a=%~d0
echo Current drive letter is: %a::=%
pause
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 10 Posted 2006-11-23 07:16 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The %a::=% statement is to achieve by removing the colon in the display result, essentially it is still intercepting the characters at specific positions.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-11-25 04:11
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
Inspired by the following post, another method to get the current drive letter was discovered.
Asking willsort and all brothers about prompt _$T$_

The code is as follows:


  1. @echo off
  2. for /f "usebackq skip=1 eol=;" %%i in (`"echo exit|%comspec% /k prompt $N$_;"`) do set "d=%%i"
  3. echo The current drive letter is: %d%
  4. pause
hxuan?表ー: 2006-11-24 15:13


There are also the following codes, which can output the current drive letter, current path, current date, current time, and version number, and can also output your personalized DOS prompt.


  1. @echo off
  2. for /f "usebackq skip=1 eol=; delims=" %%i in (`"echo exit|%comspec% /k prompt $N$_;"`) do set "aaa=%%i"
  3. echo %aaa%
  4. for /f "usebackq skip=1 eol=; delims=" %%i in (`"echo exit|%comspec% /k prompt $D$_;"`) do set "aaa=%%i"
  5. echo %aaa%
  6. for /f "usebackq skip=1 eol=; delims=" %%i in (`"echo exit|%comspec% /k prompt $E$_;"`) do set "aaa=%%i"
  7. echo %aaa%
  8. for /f "usebackq skip=1 eol=; delims=" %%i in (`"echo exit|%comspec% /k prompt $P$_;"`) do set "aaa=%%i"
  9. echo %aaa%
  10. for /f "usebackq skip=1 eol=; delims=" %%i in (`"echo exit|%comspec% /k prompt $T$_;"`) do set "aaa=%%i"
  11. echo %aaa%
  12. for /f "usebackq skip=1 eol=; delims=" %%i in (`"echo exit|%comspec% /k prompt $V$_;"`) do set "aaa=%%i"
  13. echo %aaa%
  14. pause
hxuan?表ー: 2006-11-24 15:19


[ Last edited by hxuan999 on 2006-12-13 at 11:40 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
namejm +2 2006-11-25 09:55
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 12 Posted 2006-11-25 09:54 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The `prompt` is a relatively niche command, but it has such a powerful function, which is worthy of attention from everyone. Nice nice, give hxuan999 plus points reward.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 13 Posted 2006-11-25 10:20 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Appreciate it~~
Brother hxuan999 has really rich imagination~ : )
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 14 Posted 2006-11-25 10:33 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline

The `prompt` command is relatively niche, yet it has such powerful functions, which is worthy of everyone's attention. Not bad, give hxuan999 a bonus.

Sweat, think about the DOS/9x batch processing in the past, almost all were dominated by `prompt`, and there were tons of related skills

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 15 Posted 2006-11-25 10:59 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
You can also start a new special thread for discussing Prompt techniques, which can not only carry forward the techniques but also earn more points~ Haha...
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Forum Jump: