Without Me It Just Aweso
Without Me It Just Aweso

Reputation: 4883

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

Upvotes: 6

Views: 23764

Answers (5)

eadmaster
eadmaster

Reputation: 1457

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

Upvotes: 1

aNT366 - ALICANTE
aNT366 - ALICANTE

Reputation: 11

@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

Upvotes: 0

Lucio
Lucio

Reputation: 1

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

Upvotes: -2

0xC0000022L
0xC0000022L

Reputation: 21319

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.

Upvotes: 3

Andriy M
Andriy M

Reputation: 77717

Add \ at the end of the path:

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

Upvotes: 2

Related Questions