1

I need to include an "else echo No USB detect" into this .bat (not add "if %%l NEQ 2")

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
echo USB detect in %%i
        )
        )

In simple words: if USB detected, then "echo OK". But if it does not detect the USB, then "echo No" and exit. Thanks

Solved by Tim

Important Note:

This script (by Stephan) in my opinion, is much higher to detect drives

@echo off
setlocal enabledelayedexpansion
REM get removable loaded drives:
for /f %%a in ('"wmic logicaldisk where (drivetype=2 and size is not null) get caption,size 2>nul|find ":""') do set usb=!usb! %%a
REM show a overview:
if defined usb (echo removable drives found in: %usb%) else echo no removable drive found
REM show them one-by-one:
for %%a in (%usb%) do echo removable drive found in %%a
  • 1
    So add `else (echo No USB detect)` between the two parenthesis. CMD has an `IF ELSE` structure: http://ss64.com/nt/if.html – Tim Aug 07 '16 at 20:57
  • but in which part of script? be more specific –  Aug 07 '16 at 22:06
  • In between your two close parenthesis: `)` The Syntax is `FOR .... (IF %%l equ 2 (echo USB detect in %%i) ELSE (echo No USB detected))` – Tim Aug 07 '16 at 22:41
  • btw (and Sumbul and I got in to this yesterday), batch [does not have](http://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file) an `IF ... ELSE IF...` structure. Correct syntax only allows for `IF...ELSE`. – Tim Aug 07 '16 at 22:44
  • 1
    I bet if you typed IF /? at the cmd prompt it would tell you how to do it. – Squashman Aug 07 '16 at 22:58
  • @Tim **Correct syntax is such that Cmd.exe processes without errors.** I [amended](http://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file/38820561#38820561) the thread you keep referring to. Test my examples before claiming they're wrong because someone said so. I trust my own tests and the batch experts I know, several of them post on SO. – sambul35 Aug 08 '16 at 02:57
  • Hi tim, sambul, and all. Thanks but IF ... ELSE IF or IF ... ELSE don't work. Tim (IF %%l equ 2 (echo USB detect in %%i) ELSE (echo No USB detected)) doesn't work. Sambul (IF %%l equ 2 (echo USB detect in %%i) ELSE IF %%l NEQ 2 (echo No USB detected)) doesn't work too –  Aug 08 '16 at 13:36
  • @sambul35 `Correct syntax is such that Cmd.exe processes without errors` That's where we're going to have to agree to disagree. "Working" does not equal "Correct" in my book. If that's the way you roll, then more power to you. I ain't gonna tell ya you's wrong. (Incorrect syntax imo, but you got the idea which means it works so it really must be correct, no?) – Tim Aug 08 '16 at 15:09
  • @alejc I posted a carefully tested answer below in this thread. It works simply perfect for me. Pls try it, and post any errors, or upvote and accept by clicking signs on the left of text box as usually done on SO. – sambul35 Aug 08 '16 at 15:13
  • sambul35. I tried your script and it works well, but as I said in the question: You can not use l %% IF NEQ 2 because takes many messages (8) .... (IF %%l equ 2 (echo USB detect in %%i) ELSE IF %%l NEQ 2 (echo NO USB Detect)) –  Aug 08 '16 at 16:10

3 Answers3

1

wmic has more power than you think:

@echo off
setlocal enabledelayedexpansion
REM get removable loaded drives:
for /f %%a in ('"wmic logicaldisk where (drivetype=2 and size is not null) get caption,size 2>nul|find ":""') do set usb=!usb! %%a
REM show a overview:
if defined usb (echo removable drives found in: %usb%) else echo no removable drive found
REM show them one-by-one:
for %%a in (%usb%) do echo removable drive found in %%a
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I thought about integrating `WHERE` in there to only grab USB drives, but I wasn't sure if the OP needed to list every drive for a reason. This is an excellent solution that demonstrates the power of WMIC and WQL (`is not null`...brilliant! I wouldn't have thought of that.) – Tim Aug 08 '16 at 17:54
  • This script is very good. I'll try to see if I can replace the current one. Thank you very much –  Aug 11 '16 at 13:16
  • It is better to put lock after: else (echo no removable drive found) –  Aug 11 '16 at 13:47
  • it's just two alternatives to show the result. Try it with two USB-sticks (or remove `where (drivetype=2 and size is not null)` to show you all drives - just to see what it does). – Stephan Aug 11 '16 at 13:53
  • Ok. It is better as it is in the script, because I just need to detect usb (nothing more).... Your script is much higher than I use, so I've replaced. I thank you very much for your contribution. (update question) –  Aug 11 '16 at 16:48
0

This code works for me. Save it to test.bat and run from open Cmd Prompt. It prints only disk related info, no garbage:

@echo off
setlocal enabledelayedexpansion
for /F "usebackq tokens=* skip=1" %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (set "dsk=%%i"
    if "!dsk:~-12,1!" equ "2" (echo USB disk detected in !dsk:~0,2!
    ) else if "!dsk:~-12,1!" gtr "2" (echo No USB disk detected in !dsk:~0,2!) )
exit /b
sambul35
  • 1,058
  • 14
  • 22
0

Your question was adding an ELSE clause to your IF statement. I recommended adding ELSE between your two close parenthesis. This code does exactly that:

@echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
  if %%l equ 2 (echo USB detect in %%i) ELSE (echo No USB Detect)
)

This is my output with no USB drives plugged in:

C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
No USB Detect
No USB Detect
No USB Detect

This is my output with a thumbdrive plugged in:

C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
No USB Detect
No USB Detect
USB detect in D:
No USB Detect

Here is my result of the WMIC command you used:

C:\Users\tim\Documents\Scripting Tools>wmic logicaldisk get caption,description,drivetype
Caption  Description       DriveType
C:       Local Fixed Disk  3
D:       Removable Disk    2

You might notice it echos No USB Detect 3 times. This is correct because the FOR loop you have constructed loops through each line of output. WMIC outouts one line with the headers (Caption, Description, and DriveType), a line for each drive (and C: is not DriveType 2), and then a blank line.

Your last line, In simple words: if USB detected, then "echo OK". But if it does not detect the USB, then "echo No" and exit. Seems to imply that you only want a simple Installed/Not Installed, not a yes/no for each line of output from your WMIC query. If that is the case, you don't need (or want) an ELSE. Try this instead:

@echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
  if %%l equ 2 (
    echo USB detect in %%i
    goto RestOfBatch
  )
)
echo USB not detected
:RestOfBatch

Without a usb drive plugged in:

C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
USB not detected

And with a usb drive plugged in:

C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
USB detect in D:

If neither of these produce the desired result, please define your issue more clearly. http://ss64.com/nt/ has a comprehensive list of batch commands and their syntax. You can also add /? to the end of a command to get examples. EG: IF /?

Tim
  • 2,701
  • 3
  • 26
  • 47