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.