Ramiro
Ramiro

Reputation: 47

Batch findstr with quotes

I'm trying to figure out how to modify this script to make exact searches. I need to search for a string (eg: "123.12" -- with quotes)

This is my script up to now:

findstr %1 %systemdrive%\test\data.txt

IF %ERRORLEVEL% GTR 0 (
--- TRUE- DO SOMETHING
) ELSE (
--- FALSE- EXIT
)

This is my file data.txt

"123.123" "345.345" "794.922"

(with quotes)

The problem is that findstr is finding the value parsed: 123.12, but it does not exist in my list. It's wrongly detecting 123.123 and 123.12 as part of it. That's why I would like to know if there's any way to make searches with quotes, I mean, to format the value parsed and add it quotes to search inside the text file.

I've tried "%1" with no success.

Thank you in advance !!


EDIT

I'm trying to modify a Firewall Batch Script to block certain IP's

This is the script

ipconfig | findstr "Address" | qut -d: -f 2 | qni > %systemdrive%\Qaas\host_ip.txt
findstr \"%1\" %systemdrive%\Qaas\bin\host_ip.qas || findstr \"%1\" %systemdrive%\Qaas\white_list.txt || findstr \"%1\" %systemdrive%\Qaas\blocqedips.qas

IF %ERRORLEVEL% GTR 0 (
echo "NOT FOUND"
netsh ipsec stat add filter filterlist="IP List" srcaddr=%1 dstaddr=Me description=%1 protocol=any mirrored=yes
echo Blocked IP address %1, reason - %2 connections on %3 >> %systemdrive%\Qaas\Qaas-%dw%-%mm%-%dd%-%hh%.log
echo %1 >> %systemdrive%\Qaas\blocqedips.qas
) ELSE (
echo "FOUND"
echo %1 is either host IP or already blocked earlier >> %systemdrive%\Qaas\Qaas-%dw%-%mm%-%dd%-%hh%.log
)

This is the blocked IP file: http://pastebin.com/cP4BteDM

Now, trying to block, for example, 10.02.0, I'm receiving in console "10.02.02". I guess because 10.02.0 is "instr" "10.02.02". With this problem in the FINDSTR sentence, I will never be able to ban "10.02.0" for example.

Thank you for your help, I've tried your answers with no luck :S

Upvotes: 2

Views: 4364

Answers (2)

npocmaka
npocmaka

Reputation: 57272

escape character for quotes is \" : Try this:

C:\>echo "123.123" "345.345" "794.922" | findstr "\"123.12\""
C:\>echo %errorlevel%

EDIT: I'm still not sure I truly get the question but.Here's a pretty simple example bat that I've created to test the paste.bin ip addresses:

@echo off
find """%~1"""  ips.txt 
echo %errorlevel%

with 10.02.0 I'm getting 1 and with 10.02.02 I'm getting 0.I used FIND instead of FINDSTR to avoid dot pattern trap pointed by Electro Hacker - as you have no control in the content inside %1 and it will be a little bit harder to replace . with [.] With triple quotes as the " is the escape of " .and %~1 is for dequoted value of the first argument (may be it's not necessary )

Upvotes: 1

ElektroStudios
ElektroStudios

Reputation: 20474

If you want exact searches then you can filter the start-word and end-word lines, and scape the point "." because in a RegEx means "any character", you need to use the literal character "." if you want more exact results, and to escape too the doublequotes chars.

If you want make searches without quotes you can expand the argument like this:

:: I've removed start & ending quotes of argument %1
findstr %~1 "%systemdrive%\test\data.txt" &&  (
--- TRUE- DO SOMETHING
) || (
--- FALSE- EXIT
)

This is the exact regex that you need:

FINDSTR "\<\"123[.]123\"\>"

Noticed that the first and the last quote is only to enclose the value using FINDSTR command

That matchs exactly this: "123.123" (including quotes), without any exception, that Regex search by:

\<start of word , end of word\>

PS: Sorry for my bad english, I hope this helped you.

Upvotes: 1

Related Questions