AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 132685
In Process
kdt
kdt
Asked: 2010-04-16 05:23:05 +0800 CST2010-04-16 05:23:05 +0800 CST 2010-04-16 05:23:05 +0800 CST

批处理脚本:迭代驱动器号?

  • 772

在 Windows 批处理脚本中,如何遍历与物理卷或映射共享对应的所有驱动器号?

windows batch drive-letters
  • 7 7 个回答
  • 5644 Views

7 个回答

  • Voted
  1. MattB
    2010-04-16T06:10:17+08:002010-04-16T06:10:17+08:00

    还建议进行语言切换,但要使用 Powershell 而不是 VBS。

    Get-PSDrive -PSProvider FileSystem
    

    这是未来的浪潮……

    • 3
  2. Dan Andreatta
    2010-04-16T05:34:15+08:002010-04-16T05:34:15+08:00

    也许现在有更好的工具,但曾经有fsutil命令(WinXP)。

    fsutil fsinfo drives
    

    这将返回系统中的所有驱动器。

    • 2
  3. John Gibbons
    2018-09-03T16:57:08+08:002018-09-03T16:57:08+08:00

    这应该有效。它创建了一个可从 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.
    
    • 1
  4. sinping
    2010-04-16T05:25:13+08:002010-04-16T05:25:13+08:00

    在这种情况下,使用 VBScript 会更容易,也更有用。 http://authors.aspalliance.com/brettb/VBScriptDrivesCollection.asp

    • 0
  5. Joey
    2010-04-19T05:10:37+08:002010-04-19T05:10:37+08:00

    你可以哄

    wmic volume get driveletter
    

    成为您的数据源。然后可以使用标准进行迭代for /f。

    • 0
  6. RobW
    2010-04-20T10:38:59+08:002010-04-20T10:38:59+08:00

    你可以试试:

    对于 (CDEFGHIJKLMNOP QRSTUVWXYZ) 中的 %%i 执行 @if 存在 %%i: @echo %%i:

    此代码的主要限制是存在没有磁盘的 CD/DVD 驱动器。它使用户插入磁盘。如果您将所有 CD/DVD 驱动器映射到 Z:您可以通过删除集合中的最后一个 Z 来避免鲤鱼。

    抢

    • 0
  7. davr
    2010-04-16T07:50:01+08:002010-04-16T07:50:01+08:00

    这适用于 msys,您没有指定您使用的 bash 解释器(主要是 msys 或 cygwin)。

    for i in `mount|grep "^.: "|cut -c1`; do
      echo iterating over drive $i
    done
    
    • -2

相关问题

  • 您最喜欢的云计算提供商是什么?[关闭]

  • Vanilla Powershell 是否足以成为 Windows 和 DB 服务器管理员的语言?

  • 为什么添加新驱动器后我的磁盘驱动器访问速度如此之慢?

  • 在 Windows Server 2003 下使用 wscipt 从 .asp 文件运行 .exe

  • 最佳混合环境(OS X + Windows)备份?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve