中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-15 13:38
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » How to get the current drive letter and save it in a text file?
Printable Version  7,448 / 18
Floor1 chengbiner Posted 2006-11-18 06:44
初级用户 Posts 44 Credits 105
For example, to run a software on a removable disk, but the drive letter will be different on different machines!
Floor2 不得不爱 Posted 2006-11-18 06:52
超级版主 Posts 2,044 Credits 5,310 From 四川南充
```
set a=%0
echo %a:~0,2%>temp.txt
```
Floor3 chengbiner Posted 2006-11-18 06:55
初级用户 Posts 44 Credits 105
It still doesn't seem to work!
Floor4 不得不爱 Posted 2006-11-18 07:07
超级版主 Posts 2,044 Credits 5,310 From 四川南充
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
Floor5 chengbiner Posted 2006-11-18 07:09
初级用户 Posts 44 Credits 105
Thanks, moderator. It's already okay.
Floor6 namejm Posted 2006-11-18 07:24
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Regarding the acquisition of the current drive letter, there are generally the following three schemes:

1. cd command:


2. for statement extension:


3. Parameter %~0 scheme:


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 ]
Floor7 不得不爱 Posted 2006-11-18 07:38
超级版主 Posts 2,044 Credits 5,310 From 四川南充
%~0 does not get the current path with quotes
Floor8 namejm Posted 2006-11-18 08:07
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor9 hxuan999 Posted 2006-11-23 07:10
中级用户 Posts 161 Credits 337

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?

Floor10 namejm Posted 2006-11-23 07:16
荣誉版主 Posts 1,737 Credits 5,226 From 成都
The %a::=% statement is to achieve by removing the colon in the display result, essentially it is still intercepting the characters at specific positions.
Floor11 hxuan999 Posted 2006-11-25 04:11
中级用户 Posts 161 Credits 337
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 ]
Floor12 namejm Posted 2006-11-25 09:54
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor13 redtek Posted 2006-11-25 10:20
金牌会员 Posts 1,147 Credits 2,902
Appreciate it~~
Brother hxuan999 has really rich imagination~ : )
Floor14 electronixtar Posted 2006-11-25 10:33
铂金会员 Posts 2,672 Credits 7,493

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
Floor15 redtek Posted 2006-11-25 10:59
金牌会员 Posts 1,147 Credits 2,902
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...
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023