【问题标题】:Detecting USB drives and placing drive letters into variables in a Batch file检测 USB 驱动器并将驱动器号放入批处理文件中的变量中
【发布时间】:2016-08-13 16:16:39
【问题描述】:

我正在编写一个批处理文件,该文件从所有插入的与特定磁盘标签匹配的可移动驱动器中复制文件。在这种情况下,“视频”...

现有代码仅显示检测到的驱动器并要求用户自己输入驱动器号。

不应超过 4 个驱动器,但如果检测到超过 4 个驱动器,则应显示错误。

问题:如何自动设置disk1、disk2、disk3、disk4而不是强制用户这样做?

现有代码:

echo Here are the drive letters of VIDEO cards  inserted:
for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "VIDEO"') do echo/|set /p ="%%D   "
set "drive1="
if "%drive1%"=="" if %Cards% geq 1 set /p drive1= What is the FIRST video drive letter (example: d:): 
set "drive2="
if "%drive2%"=="" if %Cards% geq 2 set /p drive2= What is the SECOND video drive letter (example: d:): 
...etc..

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    快到了。只需设置您的变量而不是 echo/|set /p 构造并实现一个计数器:

    @echo off
    setlocal enabledelayedexpansion
    set count=0
    for /f %%D in ('wmic volume get DriveLetter^, Label ^| find /i "Video"') do ( 
      set /a count+=1
      set drive!count!=%%D
    )
    echo %count% drives found:
    set drive 2>nul
    if %count% gtr 4 echo too much... 
    

    【讨论】:

      【解决方案2】:

      我有一个做同样事情的批处理文件。以下是部分代码:

      @echo off
      set yes=true
      >nul find "%yes%" F:\hold.txt && (
        echo F:/ exists
      ) || (
        echo F:/ does not exist
      )
      pause
      

      因此,在插入 F:/ 的驱动器中,我有一个名为“hold.txt”的文本文件,在该文件中,我只是在文本文件中键入了“true”。如果您了解批处理和FIND 命令,您就会明白为什么会这样。它找到文件和文本字符串,并告诉程序在上面的第四行代码中打印“F:\ exists”。希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-31
        • 2010-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-26
        • 1970-01-01
        相关资源
        最近更新 更多