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:\
To loop through all drive letters without explicitly stating them you could use
forfiles
(which is delivered with all Windows versions past Vista, I believe) and its capability to expand hex. codes0xHH
, together withexit
to set the exit code and the hidden variable=ExitCode
to convert the exit code to a hexadecimal value, like in this example code:This is quite slow though, because there are several
cmd
instances opened and closed.To loop through all available drives, including network drives and also such established by
subst
, you could use the following code, based onwmic
:To loop through all local drives, you could use the following code, again based on
wmic
:To loop through all local drives, but based on
mountvol
, you could use the following code instead:Finally, for the sake of completeness, to loop through all drives that have been established by
subst
, use the this code:Here you go, you can iterate through all drives with a for loop now.
To add onto the answer of aschipfl, here is how you can programatically generate a string variable that contains all the alphabet letters for iterating through, (both upper and lower) though its kinda clunky:
I was working on doing this to use with findstr, but it will work here as well. If you want to only generate the upper or lower letters I'll leave that exercise to the reader. After generating the string this way you can use the variable in aschipfl's answer above:
Close, but it's more like this.
EDIT: Here is a powershell way, not sure if off-topic for your needs
Loops the Upper Case Alphabet
or Lower Case Alphabet
Maybe this will work from a batch file.
The best way I found was using WMI
wmic volume get "caption"
gives just the valid drive letters... Still searching for a way to do it without external tools/libs/modules (like WMI)