【问题标题】:Windows batch script for selecting drive depent on label用于根据标签选择驱动器的 Windows 批处理脚本
【发布时间】:2018-02-14 00:59:30
【问题描述】:

我正在尝试编写一个 Windows 批处理脚本,该脚本在系统启动时在多个 Windows 10 客户端 PC 上本地运行。该脚本需要根据标签 (PHILLIPS) 选择特定的驱动器,然后将一些文件复制到其中。我尝试遍历所有可能的驱动器号 C、D、E 等,并将卷名与字符串“PHILLIPS”进行比较,但由于某种原因它不起作用。我还尝试了不同的函数,例如 :GetLabel 来获取卷的名称。

这是我的尝试:

@echo off & setlocal enableextensions
for %d in (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 (
    set target_=%d:
    ::
    call :vol %target_% label_
    if /i "%label_%" == "%PHILIPS%" (
        echo %%d%
        xcopy \\SATVIE017\Softlib\philips\dpm8000-dpm8200-dpm8500_fwf_2.02.dpm target_\
        endlocal & goto :EOF
    )
)

这是我第一次编写批处理脚本,所以我不知道要寻找什么来解决这个问题。任何建议都会非常受欢迎,因为我不想在 100 多个客户端上手动部署文件。

【问题讨论】:

  • 您的意思是驱动器的实际卷标?
  • 我猜这里用 DIR 报告的卷标...我的没有 ;) C:\Users\klindberg\Source>dir 驱动器 C 中的卷没有标签。卷序列号为 0E6E-EE7B
  • for %d 在批处理文件中您必须使用%%d
  • 我的意思是驱动器的名称,像这样:s13.postimg.org/3sypjn2o7/Unbenannt.png 字母是 E,名称是 PHILLIPS。我的字母因客户端而异,可能已分配给另一台设备,但名称始终为 PHILIPS。
  • 您得到了几个有效的答案。 It's time to accept 最有帮助的将问题标记为“已解决”。

标签: windows batch-file


【解决方案1】:

你可以试试这个:

@echo off
for %%a in (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 (
vol %%a:
for /f "tokens=1-5*" %%1 in ('vol %%a:') do (
   if "%%6"=="Philips" set "vol=%%6"
 )
)
echo I found: %vol%

您将在其中将echo I found: %vol% 替换为您的实际命令。

【讨论】:

  • 它工作!非常感谢你为我节省了很多工作!
【解决方案2】:

为什么要打开所有可能的驱动器号?

for /f "tokens=2 delims==:" %%a in ('wmic logicaldisk where volumename^="Philips" get name /value') do set "volume=%%a:"
echo your volume is %volume%

注意:我使用了几种可能的技巧之一来避免丑陋的wmic 行尾。

【讨论】:

  • 从技术上讲,他甚至没有打开所有可能的驱动器号,因为 A 和 B 也是有效的驱动器,不仅用于软盘
  • @luvuvinhphuc 从技术上讲,这是一种更好的方法。我发布的另一种方法确实在列表中的每个驱动器上执行vol
【解决方案3】:

……以及使用MountVol的类似想法:

@Echo Off
Set "_target="
For /F "Delims=\ " %%A In ('"MountVol|Find ":\""'
) Do Vol %%A 2>Nul|Find /I "PHILIPS" && (Set "_target=%%A\" & GoTo Done)
If Not Defined _target Exit /B
:Done
XCopy "<Source>" "%_target%" /<Options>

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2010-09-08
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多