FIND vs FINDSTR

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
barnabe0057
Posts: 16
Joined: 04 Aug 2017 14:20
Location: France

FIND vs FINDSTR

#1 Post by barnabe0057 » 09 Nov 2020 15:55

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 ?

aGerman
Expert
Posts: 4008
Joined: 22 Jan 2010 18:01
Location: Germany

Re: FIND vs FINDSTR

#2 Post by aGerman » 09 Nov 2020 16:08

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

barnabe0057
Posts: 16
Joined: 04 Aug 2017 14:20
Location: France

Re: FIND vs FINDSTR

#3 Post by barnabe0057 » 09 Nov 2020 16:14

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.

aGerman
Expert
Posts: 4008
Joined: 22 Jan 2010 18:01
Location: Germany

Re: FIND vs FINDSTR

#4 Post by aGerman » 09 Nov 2020 16:41

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

barnabe0057
Posts: 16
Joined: 04 Aug 2017 14:20
Location: France

Re: FIND vs FINDSTR

#5 Post by barnabe0057 » 09 Nov 2020 17:03

a bit complicated but interesting, I just understood the set "%%j" :)

Post Reply