中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-08-07 21:40
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [求助]取得當前磁碟機代號與路徑 查看 866 回复 7
楼 主 [求助]取得當前磁碟機代號與路徑 发表于 2008-03-29 12:27 ·  中国 台湾 新竹市 中华电信
初级用户
★★
积分 144
发帖 65
注册 2008-03-20 20:03
18年会员
UID 113522
性别 男
来自 Taiwan
状态 离线
請問要如何取得當前執行的磁碟機代號呢?

我參考之前我詢問過的問題
http://www.cn-dos.net/forum/viewthread.php?tid=38824&fpage=1&highlight=

slore 在二樓提出的語法
修改後

@ECHO OFF
for /f "delims=" %%i in ('DIR') do SET DISKVOL=%%~Di
ECHO Now disk vol is %DISKVOL%
PAUSE


請問是否有更好的方法呢?
因為要是根目錄有一長串檔案可是會哭的 ^^"

補個問題
傳回當前路徑的話我就完全不會了^^
可否一起指教呢?
謝謝

[ Last edited by XZ on 2008-3-29 at 01:32 PM ]
2 发表于 2008-03-29 12:43 ·  中国 江苏 常州 电信
银牌会员
★★★
积分 2,404
发帖 946
注册 2005-09-08 13:44
20年会员
UID 42345
状态 离线
'DIR'换成"%CD%"?

是不是这样?
@ECHO OFF
SET DISKVOL=%~D0
ECHO Now disk vol is %DISKVOL%
PAUSE

[ Last edited by terse on 2008-3-29 at 12:45 PM ]
本帖最近评分记录 (共 1 条) 点击查看详情
评分人分数时间
XZ +2 2008-03-29 13:29
简单!简单!再简单!
3 发表于 2008-03-29 13:31 ·  中国 台湾 新竹市 中华电信
初级用户
★★
积分 144
发帖 65
注册 2008-03-20 20:03
18年会员
UID 113522
性别 男
来自 Taiwan
状态 离线
Originally posted by terse at 2008-3-29 12:43 PM:
'DIR'换成"%CD%"?

是不是这样?
@ECHO OFF
SET DISKVOL=%~D0
ECHO Now disk vol is %DISKVOL%
PAUSE

[ Last edited by terse on 2008-3-29 at 12:45 PM ]

神人 ^^
語法縮短結果相同

要是在追加當前路徑呢?
可否可行!!
這個我就完全寫不出來了 ^^"
4 发表于 2008-03-29 13:37 ·  中国 江苏 常州 电信
银牌会员
★★★
积分 2,404
发帖 946
注册 2005-09-08 13:44
20年会员
UID 42345
状态 离线
SET DISKVOL=%~dp0
简单!简单!再简单!
5 发表于 2008-03-29 13:39 ·  中国 台湾 新竹市 中华电信
初级用户
★★
积分 144
发帖 65
注册 2008-03-20 20:03
18年会员
UID 113522
性别 男
来自 Taiwan
状态 离线
阿勒 原來是我甩白吃
看了剛剛 terse 的 %CD% 與 SET DISKVOL=%~D0 的提點
應該就要去查 SET 用法
查完才發現原來這麼簡單就可以查出當然目錄

@ECHO OFF
SET DISKVOL=%~D0
SET FLODERPAHT=%CD%
ECHO Now disk vol is %DISKVOL%
ECHO Now Path vol is %FLODERPAHT%


借問一下我稍微查了一下沒有發現 =%~D0 的用法耶
可否稍加解釋 謝謝您
6 发表于 2008-03-29 13:44 ·  中国 台湾 新竹市 中华电信
初级用户
★★
积分 144
发帖 65
注册 2008-03-20 20:03
18年会员
UID 113522
性别 男
来自 Taiwan
状态 离线
剛用力找了一下GOOGLE
有查到一些資料有需要的大家參考看看
BTW: 0 與 1的差別在哪? ^^"

The following table lists the modifiers you can use in expansion.

Modifier Description
%~1 Expands %1 and removes any surrounding quotation marks ("").
%~f1 Expands %1 to a fully qualified path name.
%~d1 Expands %1 to a drive letter.
%~p1 Expands %1 to a path.
%~n1 Expands %1 to a file name.
%~x1 Expands %1 to a file extension.
%~s1 Expanded path contains short names only.
%~a1 Expands %1 to file attributes.
%~t1 Expands %1 to date and time of file.
%~z1 Expands %1 to size of file.
%~$PATH:1 Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found, this modifier expands to the empty string.

The following table lists possible combinations of modifiers and qualifiers that you can use to get compound results.

Modifier Description
%~dp1 Expands %1 to a drive letter and path.
%~nx1 Expands %1 to a file name and extension.
%~dp$PATH:1 Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.
%~ftza1 Expands %1 to a dir-like output line.

Note

In the previous examples, you can replace %1 and PATH with other batch parameter values.
The %* modifier is a unique modifier that represents all arguments passed in a batch file. You cannot use this modifier in combination with the %~ modifier. The %~ syntax must be terminated by a valid argument value.

You cannot manipulate batch parameters in the same manner that you can manipulate environment variables. You cannot search and replace values or examine substrings. However, you can assign the parameter to an environment variable, and then manipulate the environment variable.
7 发表于 2008-03-29 15:44 ·  中国 香港 腾讯云
银牌会员
★★★
积分 1,436
发帖 739
注册 2007-10-11 17:44
18年会员
UID 99469
性别 男
状态 离线
何必到google去查呢,

微软系统自带就有的。

for /?

就可以查到的
8 发表于 2008-03-29 16:42 ·  中国 台湾 新竹市 中华电信
初级用户
★★
积分 144
发帖 65
注册 2008-03-20 20:03
18年会员
UID 113522
性别 男
来自 Taiwan
状态 离线
Originally posted by abcd at 2008-3-29 03:44 PM:
何必到google去查呢,

微软系统自带就有的。

for /?

就可以查到的

原來這兩個是一樣的喔
我一直以為 那個大寫的 I 和數字是不一樣的ˇˇ

如果要在批次程式中使用 FOR 命令,請指定 %%variable,而不要指定
%variable。 變數名稱有大小寫的區分,所以 %i 不同於 %I。

還是不懂 I 為什麼可以變成數字@@a
我在努力悟看看好了
论坛跳转: