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
2.2k views
in Technique[技术] by (71.8m points)

windows - For loop in CMD... how to loop A to Z (for drive letters)

How can I get drive the valid drive letters from A to Z with the "for loop" in windows command line (cmd.exe)?

Example, list all files in a drive root folder, should be something like (conceptual):

for %f in (A..Z) do dir %f:

or aproximating existing functionality:

for /L in (A, Z, 1) do echo %f:

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Close, but it's more like this.

for %%p in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if not exist %%p:
ul set FREEDRIVELETTER=%%p

EDIT: Here is a powershell way, not sure if off-topic for your needs

Loops the Upper Case Alphabet

65..90 | foreach {[char]$_;Write-Host "Do Something"}

or Lower Case Alphabet

97..122 | foreach {[char]$_;Write-Host "Do Something"}

Maybe this will work from a batch file.

@ECHO OFF
start /b /wait powershell.exe "97..122 | foreach {$a=[char]$_ ;dir $a:}"
PAUSE

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

...