Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
793 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...