check if a removable drive exists before running

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aristosv
Posts: 19
Joined: 07 Nov 2013 01:57

check if a removable drive exists before running

#1 Post by aristosv » 30 May 2016 23:51

Hi,

I am using this, to eject a removable drive.

Code: Select all

echo Removing Drive
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
   for %%c in (%%b) do (
      for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
         if %%d equ Removable (
         %~dp0RemoveDrive.exe %%c -L -b
         )
      )
   )
)


But if a removable drive does not exists, there's no point in running this. So I need to check if a removable drive exists, and if it doesn't, echo a message saying that a removable drive does not exist.

how can I do this?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: check if a removable drive exists before running

#2 Post by penpen » 31 May 2016 02:24

I don't understand:
The program fsutil lists only existing drives.

Edit: I completely misunderstood your post; sorry.


penpen
Last edited by penpen on 31 May 2016 02:47, edited 1 time in total.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: check if a removable drive exists before running

#3 Post by elzooilogico » 31 May 2016 02:26

Edit
pepen wrote:I don't understand:
The program fsutil lists only existing drives.


You're are right. But this may be used for our benefit.


aristosv wrote:But if a removable drive does not exists, there's no point in running this. So I need to check if a removable drive exists, and if it doesn't, echo a message saying that a removable drive does not exist.

I think you have all you need. But you may perform two steps

Code: Select all

@echo off
SetLocal EnableDelayedExpansion

set "drives="

for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
  for %%c in (%%b) do (
    for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
      if %%d equ Removable (
        set "drives=!drives!%%c "
      )
    )
  )
)

if "%drives%" NEQ "" (
   echo Removing Drives %drives%
   for %%c in (%drives%) do %~dp0RemoveDrive.exe %%c -L -b
) else (
   echo No removable drives detected
)

EndLocal
In the first step we check if there is any removable device present. If succeded, update drives var. After, if drives var isn't empty we can now do desired action, else echo message.
BTW as drives var is updated inside a block, we must use delayed expansion and rewrite %drives% as !drives!.

Hope it helps.

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: check if a removable drive exists before running

#4 Post by douglas.swehla » 01 Jun 2016 13:33

You've got basically two ways to approach this:
Option 1, you test all the drives, remove those you can, and output a message only if none were removed.
Option 2, you test for removable drives, remove any that exist, or output a message if there are none.

For Option 1, you're pretty much there already. I'd just add a counter of drives removed, and output the message if the counter is still zero when you're done.

Code: Select all

echo Removing Drive
set /a removed=0
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
   for %%c in (%%b) do (
      for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
         if %%d equ Removable (
            %~dp0RemoveDrive.exe %%c -L -b && set /a removed+=1 || @echo Could not remove drive %%c
         )
      )
   )
)
if %removed% equ 0 echo No removable drives found.


If you really want to check for the existence of any removable drives at all, then you can use a variation on the WMIC command discussed in this Stack Overflow post, which references work by dbenham, and this article on drive letters, which describes the DriveType numbers.

Code: Select all

echo Removing Drive
set /a removed=0
for /f %%D in ('
    wmic logicaldisk where "DriveType=2" get name 2^>NUL ^| find ":"
') do (
   %~dp0RemoveDrive.exe %%D -L -b && set /a removed+=1 || @echo Could not remove drive %%D
)
if %removed% equ 0 echo No removable drives found.


If the WMIC query finds no removable drives, it sends the text "No Instance(s) Available." to the standard error stream. In the code above, we use redirection to send the error to NUL, so that we can focus on positive results. You could rewrite this to check for the error message, and if it's present, show it and break out of the loop. That would remove the need for a "removed" count.

Keep in mind that WMIC is a very powerful, very large program, and can take a second to fire up, so unless you have a lot of drives to iterate over, you might get faster performance with the first version.

Post Reply