【问题标题】:For loop in CMD... how to loop A to Z (for drive letters)CMD中的循环...如何循环A到Z(用于驱动器号)
【发布时间】:2014-02-20 01:26:15
【问题描述】:

如何使用 Windows 命令行 (cmd.exe) 中的“for loop”获取从 A 到 Z 的有效驱动器号?

例如,列出驱动器根文件夹中的所有文件,应该类似于(概念):

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

或近似现有功能:

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

【问题讨论】:

  • 你没有解释你到底想要做什么,但也许这就是你真正想要的:How to get a list of drive letters on a system through a windows shell (bat, cmd)?
  • 我想要做的正是它在问题中所说的......从命令控制台获取从 A 到 Z 的有效驱动器号......以可能的简单方式......最好不要使用 PowerShell、WMI、任何 3rd 方工具......为了更好地理解,我将发布一个关于如何使用 WMI 获得我想要的东西的答案......
  • 我发现最好的方法是使用 WMI wmic volume get "caption" 只提供有效的驱动器号...仍在寻找一种无需外部工具/库/模块(如 WMI)的方法

标签: windows for-loop command-line cmd


【解决方案1】:

关闭,但更像是这样。

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

编辑:这是一种 powershell 方式,不确定是否符合您的需求

循环大写字母

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

或小写字母

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

也许这适用于批处理文件。

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

【讨论】:

  • 那是我试图简化的那个...谢谢... ;-)
  • 没见过更简单的纯批处理版本,添加了一个powershell以防万一。
  • 我试图尽可能地远离 powershell ......但你提出的解决方案 char(97..122) 是我期望在 *.cmd 文件中得到的! !!谢谢你的线索...
  • 反之亦然也很好:'@ for %p in (ABCDEFGHIJKLMNOPQRSTU VWXYZ) do @ if exists %p:\nul @ fsutil fsinfo drivetype %p:'(@ 后面不需要空格)
  • 65..90 | foreach { $d = [char]$_; New-Item -Path "c:\users\xxx\test" -Name $d -ItemType "directory"; }
【解决方案2】:

要遍历所有驱动器号而不明确说明它们,您可以使用forfiles(我相信所有 Windows 版本都提供了它)及其扩展十六进制的能力。代码0xHH,与exit 一起设置exit codehidden variable =ExitCode 将退出代码转换为十六进制值,如本示例代码:

@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:\
)

虽然这很慢,因为打开和关闭了几个cmd instances


要遍历所有可用驱动器,包括网络驱动器和subst建立的驱动器,您可以使用以下代码,基于wmic

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

要遍历所有本地驱动器,您可以使用以下代码,同样基于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\

要遍历所有本地驱动器,但基于mountvol,您可以改用以下代码:

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

最后,为了完整起见,循环通过subst建立的所有驱动器,使用以下代码:

for /F "delims=\" %%C in ('subst') do echo %%C\

【讨论】:

  • 感谢您的回答...我不知道“forfiles”命令的存在...(我总是使用 FOR /F /D /L 等)我会检查一下多一点......我也总是忘记有一个'mountvol'命令:-)......对于这项工作,我发现WMI / WMIC解决方案更优雅......再次感谢您回答的完整性......
  • 感谢这对我帮助很大。此外,要列出除 DVD 或 CD 之外的驱动器:wmic logicaldisk where drivetype=3 get caption(驱动器类型 3 是本地磁盘)``` 0 未知 1 无根目录 ​​2 可移动磁盘 3 本地磁盘 4 网络驱动器 5 光盘 6 RAM 磁盘 ```
【解决方案3】:

我发现最好的方法是使用 WMI

wmic volume get "caption"

只给出有效的驱动器号... 仍在寻找一种无需外部工具/库/模块(如 WMI)的方法

【讨论】:

    【解决方案4】:

    要补充 aschipfl 的答案,您可以通过以下方式以编程方式生成一个字符串变量,其中包含所有用于迭代的字母(包括上下),尽管它有点笨重:

    @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  
    

    我正在努力将其与 findstr 一起使用,但它也可以在这里工作。如果您只想生成大写或小写字母,我会将这个练习留给读者。 以这种方式生成字符串后,您可以使用上面 aschipfl 答案中的变量:

    for %%p in (%alpha%) do if not exist %%p:\nul set FREEDRIVELETTER=%%p
    

    【讨论】:

      【解决方案5】:

      现在,您可以使用 for 循环遍历所有驱动器。

      @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.
      

      【讨论】:

        猜你喜欢
        • 2022-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多