locked
FINDSTR how to display RRS feed

  • Question

  • Hello,

    I have a list.txt with strings one string per line(about 100 strings), and I want to search them in about 1000 files.

    This batch script is do this, but it only writes the filenames in result.txt file, but not the strings, which string was found in which file.

    @echo off
    findstr /S /G:C:\strings_to_search.txt C:\folder_search_in.* > C:\results.txt
    if %errorlevel%==0 (
    echo Found! logged files into C:\results.txt
    ) else (
    echo No matches found
    )

    --------------------------------

    Ques.?

    How to do it, like this

    string_what_I_search_for - c:\filename_where_the_string_was_found.txt

    string1_what_I_search_for - c:\filename1_where_the_string_was_found.txt

    string2_what_I_search_for - c:\filename2_where_the_string_was_found.txt

    etc....

    Friday, November 28, 2014 2:35 PM

Answers

  • Thank you for your fast reply!

    One more thing. How to specify the directory (recursively) where I search the strings?

    Thank you!


    Here you go. Make sure to use a drive letter for the folder name to be searched.

    @echo off
    if exist c:\results.txt del c:\results.txt
    for /F "tokens=1,2 delims=:" %%a in ('findstr /s "SearchString" d:\FolderName\*.txt') do (
       echo %%a%%b
    >> c:\results.txt
    )
    if exist c:\result.txt
       echo Found! logged files into C:\results.txt
    ) else (
        echo No matches found
    )

    • Marked as answer by Tomori Anna Monday, December 1, 2014 5:11 PM
    Friday, November 28, 2014 10:28 PM

All replies

  • Here you go:

    @echo off
    if exist c:\results.txt del c:\results.txt
    for %%a in (c:\Tools\*.bat) do @find /i "SearchString" "%%a" > nul && echo %%a >> c:\results.txt
    if exist c:\result.txt
       echo Found! logged files into C:\results.txt
    ) else (
        echo No matches found
    )

    Friday, November 28, 2014 3:30 PM
  • Thank you for your fast reply!

    One more thing. How to specify the directory (recursively) where I search the strings?

    Thank you!


    • Edited by Tomori Anna Friday, November 28, 2014 10:11 PM
    Friday, November 28, 2014 10:03 PM
  • Thank you for your fast reply!

    One more thing. How to specify the directory (recursively) where I search the strings?

    Thank you!


    Here you go. Make sure to use a drive letter for the folder name to be searched.

    @echo off
    if exist c:\results.txt del c:\results.txt
    for /F "tokens=1,2 delims=:" %%a in ('findstr /s "SearchString" d:\FolderName\*.txt') do (
       echo %%a%%b
    >> c:\results.txt
    )
    if exist c:\result.txt
       echo Found! logged files into C:\results.txt
    ) else (
        echo No matches found
    )

    • Marked as answer by Tomori Anna Monday, December 1, 2014 5:11 PM
    Friday, November 28, 2014 10:28 PM
  • It does not work...  :(

    Where can I specify the strings.txt?

    I have a strings.txt file, containing the words I want to search for.

    I have C:/somefolder/*.*  files where I want to find the words in strings.txt

    What am I doing wrong?

    Friday, November 28, 2014 11:15 PM
  • I have C:/somefolder/*.*  files where I want to find the words in strings.txt


    It's the word SearchString in my code . . .
    Note also that you've got the wrong type of slashes in the line above. The console uses forward slashes for switches, backslashes for folders.
    Friday, November 28, 2014 11:19 PM
  • Syntax error... Sorry I give it up.

    Thank you for your time!


    Sure it is c:\somefolder\
    • Edited by Tomori Anna Saturday, November 29, 2014 12:12 AM
    Friday, November 28, 2014 11:38 PM
  • Syntax error... Sorry I give it up.

    This is the version I tested. It works flawlessly. You should post your version too!

    @echo off
    for /F "tokens=1,2 delims=:" %%a in ('findstr /s "registry" d:\tools\*.bat') do echo %%a%%b >> d:\test.txt

    • Proposed as answer by Frederik Long Saturday, November 29, 2014 11:22 PM
    Saturday, November 29, 2014 12:21 AM
  • I could solve this problem using grep and regexp in bash/linux. Sorry, this batch script was difficult for me. I have no coding skills at all. Sorry!

    Thank you so much for your time!

    Monday, December 1, 2014 5:10 PM
  • No need to struggle with cmd.exe shell script (batch) syntax and its thorny details. Use PowerShell.


    -- Bill Stewart [Bill_Stewart]

    Monday, December 1, 2014 5:20 PM