9

How to search for the exact match of string(s) using the windows findstr command? For example: I need to find only the exact match the string store but not stored, storeday, etc.

The below command returns all strings, store, stored and storeday:

findstr /l /s /i /m /c:"store" "c:\test\*.txt"

Complete script:

set "manifest_folder=C:\Calc_scripts*.*"
set "file_list=C:\Search_results\Search_Input.txt"
set "outputfile=C:\Search_results\Search_results.txt"
(for /f "usebackq delims=" %%a in ("%file_list%") do (
    set "found="
    for /f "delims=" %%b in ('findstr /r /s /i /m /c:"%%a" "%manifest_folder%"') do (
        echo %%a is found in %%~nxb
        set "found=1"
    )
    if not defined found (
        echo %%a is not found
    )
))> "%outputFile%" 
aschipfl
  • 33,626
  • 12
  • 54
  • 99
m.joe
  • 105
  • 2
  • 2
  • 8
  • Put a space at the start and end of the string? eg. findstr /l /s /i /m /c:" store " "c:\test*.txt" – Davy C Aug 09 '16 at 18:04
  • 1
    `findstr /R /S /I /M /C:"\" "C:\test*.txt"` – aschipfl Aug 09 '16 at 18:16
  • @DavyC : adding a space would not work because I am searching the string in a list of say calc scripts. So the string 'stored' would be in different variations (not just space) eg : "store" , ->"store"->, "store";, @function(store).. etc.. etc.. – m.joe Aug 09 '16 at 18:54
  • @aschipfl : I tried it, but it says none of the strings are found. Below the part from my script where I use the findstr, 'findstr /r /s /i /m /c:"<\%%a\>" "%manifest_folder%"' . Did I do it right? – m.joe Aug 09 '16 at 19:10
  • 1
    @m.joe, without seeing your whole script and some of the input it is hard to debug. We have no idea what those variables are expanding to. – Squashman Aug 09 '16 at 19:27
  • Here you go..set "manifest_folder=C:\Calc_scripts\*.*" set "file_list=C:\Search_results\Search_Input.txt" set "outputfile=C:\Search_results\Search_results.txt" (for /f "usebackq delims=" %%a in ("%file_list%") do ( set "found=" for /f "delims=" %%b in ('findstr /r /s /i /m /c:"\<%%a\>" "%manifest_folder%"') do ( echo %%a is found in %%~nxb set "found=1" ) if not defined found ( echo %%a is not found ) ))> "%outputFile%" – m.joe Aug 09 '16 at 19:36
  • Check the positions of the backslashes in the search string, it must read: `"\<%%a\>"` (`\<` and `\>` mark word boundaries, according to the help of `findstr /?`; most characters other than letter, numerals and `_` are considered as word separators)... And please [edit your question](http://stackoverflow.com/posts/38857449/edit) and post your script there! – aschipfl Aug 09 '16 at 19:36
  • @m.joe you are supposed to edit your question with code updates. – Squashman Aug 09 '16 at 22:04
  • @aschipfl : Thanks! it worked. How do I mark your answer correct ?(sorry, new to stack overflow) – m.joe Aug 25 '16 at 14:25
  • You can't, because my "answer" is actually a comment. As soon as you [edit your question](http://stackoverflow.com/posts/38857449/edit) and put your script there instead of in the [comment above](http://stackoverflow.com/questions/38857449/how-to-search-for-the-exact-match-of-strings-using-the-windows-findstr-comman#comment65082226_38857449) and let me know, I can prepare a complete answer and post it as a real answer, which you could accept by clicking the green checkmark. – aschipfl Aug 25 '16 at 14:38
  • I formatted the code as you copied it as a single line only which is hardly readable; please check whether my edit truly reflects your script and let me know... thanks! – aschipfl Aug 30 '16 at 19:12
  • @aschipfl: That's perfect. Thanks! – m.joe Aug 30 '16 at 19:59

1 Answers1

8

According to the findstr /? help, \< and \> denote word boundaries -- see the following excerpt:

Regular expression quick reference:
  .        Wildcard: any character
  *        Repeat: zero or more occurrences of previous character or class
  ^        Line position: beginning of line
  $        Line position: end of line
  [class]  Character class: any one character in set
  [^class] Inverse class: any one character not in set
  [x-y]    Range: any characters within the specified range
  \x       Escape: literal use of metacharacter x
  \<xyz    Word position: beginning of word
  xyz\>    Word position: end of word

Hence you need to change your findstr command line like this:

findstr /r /s /i /m /c:"\<store\>" "c:\test\*.txt"

So in your complete script it should look like this:

findstr /r /s /i /m /c:"\<%%a\>" "%manifest_folder%"
aschipfl
  • 33,626
  • 12
  • 54
  • 99