Page 1 of 1

FIND vs FINDSTR

Posted: 09 Nov 2020 15:55
by barnabe0057
Hi guys,
These 2 lines allow me to detect if a removable disk (a usb key named "USB1") is connected:

Code: Select all

>"%TEMP%\usb_devices.txt" wmic logicaldisk where drivetype=2 get DeviceID, VolumeName

find /i "USB1" "%TEMP%\usb_devices.txt"
The FIND command finds the string "USB1" but if I replace FIND with FINDSTR, oddly the FINDSTR command does not find anything.
What could be the reason of this difference in result ?

Re: FIND vs FINDSTR

Posted: 09 Nov 2020 16:08
by aGerman
If wmic output is redirected to a file it will be UTF-16-encoded. While FIND can handle it, FINDSTR obviously can't.
What's your goal? I'm pretty sure we can work around this issue without the temporary file.

Steffen

Re: FIND vs FINDSTR

Posted: 09 Nov 2020 16:14
by barnabe0057
I asked the question to satisfy my curiosity, I can continue to use FIND + the temporary file, no problem for me.
Thank you very much for your responsiveness.

Re: FIND vs FINDSTR

Posted: 09 Nov 2020 16:41
by aGerman
You could process the output in a FOR /F loop without the temporary file. And WQL supports AND for the filter.

Code: Select all

@echo off &setlocal
set "DeviceID="
for /f %%i in ('2^>nul wmic logicaldisk WHERE "drivetype=2 AND VolumeName='USB1'" get DeviceID /value') do for /f %%j in ("%%i") do set "%%j"
if defined DeviceID (echo %DeviceID%) else echo not found
pause
The second FOR loop is necessary to remove the extra carriage return character at the end of the line (WMIC bug).

Steffen

Re: FIND vs FINDSTR

Posted: 09 Nov 2020 17:03
by barnabe0057
a bit complicated but interesting, I just understood the set "%%j" :)