开发者

batch script to find drive letter of a mounted device

I am trying to write a batch script to locate a particular mounted device. I'm in 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

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?

thanks,

Stephanie


Use fsutil fsinfo drives inside the for statement instead of a static list of drive letters.

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 may still 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.


Add \ at the end of the path:

IF EXIST %%d:\custom\ (...)


A bit complicated, but this is the only solution to avoid blocking errors on 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):

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


@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


Try this:

if exist %%d:\nul (
   echo Device found %%d
)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜