简体   繁体   中英

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.

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

在路径末尾添加\\

IF EXIST %%d:\custom\ (...)
@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): %%?:\ 
)

PS: Run WinXP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM