windows 用于查找已安装设备的驱动器号的批处理脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5709189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
batch script to find drive letter of a mounted device
提问by Without Me It Just Aweso
I am trying to write a batch script to locate a particular mounted device. I'm in windows 7.
我正在尝试编写一个批处理脚本来定位特定安装的设备。我在 Windows 7 中。
I know that the device will have the folder drive:\custom so I'm wanting to look at all possabilities to find a device with this path
我知道该设备将具有文件夹 drive:\custom 所以我想查看所有可能性以找到具有此路径的设备
Here is what i have so far
这是我到目前为止所拥有的
@echo off
setLocal Enabledelayedexpansion
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 (
if exist %%d:\custom (
ECHO Device Found : %%d
)
)
This doesnt work though, it thinks it exists for every drive letter.. so i see 'Device Found' for every single drive letter. Why is that? Am I going about this wrong? How can I locate the drive letter that has a folder 'custom' on the root directory?
但这不起作用,它认为它存在于每个驱动器号..所以我看到每个驱动器号的“设备找到”。这是为什么?我这样做是错误的吗?如何在根目录中找到包含“custom”文件夹的驱动器号?
thanks,
Stephanie
谢谢,
斯蒂芬妮
采纳答案by Andriy M
Add \
at the end of the path:
\
在路径末尾添加:
IF EXIST %%d:\custom\ (...)
回答by 0xC0000022L
Use fsutil fsinfo drives
inside the for
statement instead of a static list of drive letters.
fsutil fsinfo drives
在for
语句中使用而不是驱动器号的静态列表。
for /f "tokens=1,*" %%i in ('fsutil fsinfo drives') do (
:: work with %%j here
)
However, if a drive letter is given to a device with no media, it maystill give an error. Either way, a check such as:
但是,如果为没有媒体的设备提供驱动器号,它仍可能会出现错误。无论哪种方式,检查,如:
if not exist O:\ @echo test
worked perfectly fine for me (with and without not
). The drive does not exist on my system, so no output was given when the not
got removed.
对我来说工作得很好(有和没有not
)。该驱动器在我的系统上不存在,因此not
在移除该驱动器时没有给出任何输出。
回答by eadmaster
A bit complicated, but this is the only solution to avoid blocking errors on Win7:
有点复杂,但这是在 Win7 上避免阻塞错误的唯一解决方案:
for /f "tokens=3" %%d in ('echo LIST Volume ^| DISKPART ^| findstr "Healthy Unusable"') do (
if exist %%d:\custom echo Device found
)
Another method i've found is using the vol
command + checking ERRORLEVEL
(if == 1 the drive is not mounted):
我发现的另一种方法是使用vol
命令 + 检查ERRORLEVEL
(如果 == 1 驱动器未安装):
for /f "tokens=3" %%d in ('echo LIST Volume ^| DISKPART ^| findstr "Healthy Unusable"') do (
vol %%d:
if !ERRORLEVEL!==0 if exist %%d:\custom echo Device found
)
NOTE: on WinXP DISKPART
won't see removable drives...
注意:在 WinXPDISKPART
上看不到可移动驱动器...
回答by aNT366 - ALICANTE
@ECHO OFF
:CICLO
CLS&ECHO.&ECHO VER ESTADO UNIDADES CON WMIC
SET "DVF="
FOR /F "tokens=1,*" %%A IN ('wmic logicaldisk get caption^, description ^| FIND ":"') DO (
VOL %%A >nul 2>&1 && (
CALL SET "DVF=%%DVF%% %%A"& ECHO %%A ^| ON. %%B) || (
ECHO %%A ^| OFF. %%B
)
)
ECHO DEVICEFOUND: %DVF%
TIMEOUT /T 5 >NUL
GOTO:CICLO
回答by a_horse_with_no_name
Try this:
尝试这个:
if exist %%d:\nul (
echo Device found %%d
)
回答by Lucio
This works for a hard disk and a pendrive:
这适用于硬盘和随身碟:
@echo off
for %%? 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 (
dir %%?:\ > nul 2>nul
if exist %%?:\custom echo Device found(s): %%?:\
)
P.S.: Run WinXP
PS:运行WinXP