Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

windows - Refer to/select a drive based only on its label? (i.e., not the drive letter)

I'm trying to refer to a drive whose letter may change. I'd like to refer to it by its label (e.g., MyLabel (v:) within a Batch File. It can be referred to by V: . I'd like to refer to it by MyLabel.

(This was posted on Experts Echange for a month with no answer. Let's see how fast SO answers it )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The previous answers seem either overly complicated, and/or not particularly suited to a batch file.

This simple one liner should place the desired drive letter in variable myDrive. Obviously change "My Label" to your actual label.

for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "My Label"') do set myDrive=%%D

If run from the command line (not in a batch file), then %%D must be changed to %D in both places.

Once the variable is set, you can refer to the drive using %myDrive%. For example

dir %myDrive%someFolder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...