For loop in CMD… how to loop A to Z (for drive letters)

前端 未结 5 1906
眼角桃花
眼角桃花 2020-12-20 17:41

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, shou

相关标签:
5条回答
  • 2020-12-20 18:14

    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:\nul 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
    
    0 讨论(0)
  • 2020-12-20 18:15

    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)

    0 讨论(0)
  • 2020-12-20 18:21

    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:

    @ECHO OFF
    setlocal enabledelayedexpansion
    FOR /F "tokens=1,2" %%a  IN ('ECHO 65 90 ^& ECHO 97 122') DO (
    FOR /L %%i IN (%%a,1,%%b) DO (cmd /c exit %%i & set alpha=!alpha! !=exitcodeAscii!))
    echo %alpha%
    pause  
    

    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:

    for %%p in (%alpha%) do if not exist %%p:\nul set FREEDRIVELETTER=%%p
    
    0 讨论(0)
  • 2020-12-20 18:31

    Here you go, you can iterate through all drives with a for loop now.

    @echo off
    setlocal enableDelayedExpansion
    cls
    
    REM getting the output from fsutil fsinfo drives and putting it in the ogdrives variable
    FOR /F "tokens=* USEBACKQ" %%F IN (`fsutil fsinfo drives`) DO (
    SET ogdrives=%%F
    )
    REM making the drives variable the same as the ogdrives variable so it can be manipulated
    set drives=!ogdrives!
    
    REM formating the out so that it looks like 1+1+1+... for every drive that is connected
    set drives=!drives:Drives^: =!
    set drives=!drives:^:\=1!
    set drives=!drives: =+!
    
    REM still formating to find out how many drives there are, this bit gets rid of any letters there are
    set charms=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    for /L %%N in (10 1 62) do (
    for /F %%C in ("!charms:~%%N,1!") do (
    set drives=!drives:%%C=!
    )
    )
    REM last for finding out the number, this removes the last characters since it is a leading + that shouldnt be there
    set drives=!drives:~0,-1!
    REM num is now the variable that contains the number of drives connected
    set /a num=!drives!
    
    REM reseting the drives variable to the original output so it can be manipulated again
    set drives=!ogdrives!
    REM this time it is being formated to list the drives as a solid string of drive letters like ABCD
    set drives=!drives:Drives^: =!
    set drives=!drives:^:\=!
    set drives=!drives: =!
    
    REM this is to iterate through that string of drive letters to seperate it into multiple single letter variables that are correlated to a number so they can be used later
    :loop
    REM the iter variable holds how many times this has looped so that when it hits the final drive it can exit
    set /a iter=!iter!+1
    REM the pos variable is the position in the string of drive letters that needs to be taken out for this iteration
    set /a pos=!iter!-1
    
    REM this sets the driveX variable where X is the drives correlated number to the letter of that drive from the long string of drive letters by using the pos variable
    set drive!iter!=!drives:~%pos%,1!
    
    REM this is checking to see if all drives have been assigned a number and if it has it will exit the loop
    if !iter!==!num! goto oloop
    goto loop
    :oloop
    
    
    REM drives are stored in variables %driveX% where X represents the drive number
    REM the number of drives are stored in the %num% variable
    REM below is an example for iterating through drives
    
    REM this is an example of how to use the information gathered to iterate through the drives
    REM we are using a for loop from 1 to the number of drives connected
    for /L %%n in (1 1 !num!) do (
    REM for every drive that is connected this will be ran
    REM %%n contains a number which will increase since its a for loop
    REM the drive driveX variable can then be used since drive1=A and drive2=B etc
    echo drive %%n is !drive%%n!
    REM you can see how i have embedded a variable inside a variable to create an array of sorts.
    )
    pause
    exit
    REM the actual variable names are drive1 drive2 drive3 but so that we can iterate through them we can just use a for loop and put the number in the variable
    REM one way you can use this is with the where command since it will only search one drive at a time
    REM you can do this like this 
    
    for /L %%n in (1 1 !num!) do (
    
    where /R !drive%%n!:\ *.txt
    
    )
    
    REM this will return a list of all txt files in the entire system since it searches all drives.
    
    0 讨论(0)
  • 2020-12-20 18:34

    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. codes 0xHH, together with exit to set the exit code and the hidden variable =ExitCode to convert the exit code to a hexadecimal value, like in this example code:

    @echo off
    for /L %%C in (0x41,1,0x5A) do (
        cmd /C exit %%C
        for /F %%D in ('
            forfiles /P "%~dp0." /M "%~nx0" /C "cmd /C echo 0x%%=ExitCode:~-2%%"
        ') do echo %%D:\
    )
    

    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 on wmic:

    for /F "skip=1" %%C in ('wmic LogicalDisk get DeviceID') do for /F %%D in ("%%C") do echo %%D\
    

    To loop through all local drives, you could use the following code, again based on wmic:

    for /F "skip=1" %%C in ('wmic Volume where "DriveLetter is not Null" get DriveLetter') do for /F %%D in ("%%C") do echo %%D\
    

    To loop through all local drives, but based on mountvol, you could use the following code instead:

    for /F %%C in ('mountvol ^| find ":\"') do echo %%C
    

    Finally, for the sake of completeness, to loop through all drives that have been established by subst, use the this code:

    for /F "delims=\" %%C in ('subst') do echo %%C\
    
    0 讨论(0)
提交回复
热议问题