Solved problem with findstr - i need to get the searched string!

June 5, 2012 at 19:22:14
Specs: Windows XP, P4 2GB
Hi.
I have a script to do a findstr, using an input file with a list of the various search terms, and spit the filename to an ouput file.

for /f "delims=" %%a in ('findstr /S /I /M /G:"X:\stringslisting.txt" *.doc') do (dir /s /b "%%~fa" >> X:\OutputFile.txt)

eg: X:\stringslisting.txt
string1
string2

And it works fine, giving me a file with a list of all the path\filename\ of fils containing the strings as passed from the X:\stringslisting.txt file.

eg:
c:\path1\file1.doc
c:\path2\file2.doc

Step 2)
I want to be able to also output the searched string for each file.
eg
String1
c:\path1\file1.doc
String2
c:\path2\file2.doc

or
String1=c:\path1\file1.doc
String2=c:\path2\file2.doc
(using "=" as the delimeter between the search term and the file it finds.

PROBLEM: I can't find how to get hold of the inputted search string during each iteration, to be ble to pump it to the output file !!

PLEASE HELP !!
Any assistace is greatly appreciated.
Many thanks, Eli from Oz


See More: problem with findstr - i need to get the searched string!

Report •

✔ Best Answer
June 14, 2012 at 04:05:54
How about:

@ECHO OFF >X:\OutputFile.txt

FOR /F "tokens=*" %%A IN (X:\stringslisting.txt) DO (
    >>X:\OutputFile.txt ECHO Search String: "%%~A"
    >>X:\OutputFile.txt FINDSTR /S /I /M /C:"%%~A" "*.DOC"
    >>X:\OutputFile.txt ECHO(
)
EXIT /B

The above is less efficient since it has to open all the doc files multiple times (once for each search string), opposed to opening each doc file once and look for all search strings in the input text file, but I think it will provide what you are looking for!

When your only tool is a hammer, every problem looks like a nail.



#1
June 7, 2012 at 14:42:21
can you just add

for /f "delims=" %%a in ('findstr /S /I /M /G:"X:\stringslisting.txt" *.doc') do (echo %%a >> X:\OutputFile.txt && dir /s /b "%%~fa" >> X:\OutputFile.txt)

:: mike


Report •

#2
June 13, 2012 at 18:21:17
Sorry it took so long to reply. (I didn't receive an email telling me there was a reply to my question ?!?!)

Anyways,

Thanks for replying. But no cigar.
I had already tried that, (to echo %%a into the file aswell)
But that gives me a result like this:
eg
path1\file1.doc
c:\path1\file1.doc
path2\file2.doc
c:\path2\file2.doc

But I DON't want the 'found path\filename' displayed twice, I actually want the 'searched string', and then followed by the found drive\path\file.

eg
String1
c:\path1\file1.doc
String2
c:\path2\file2.doc

ie, for the output file to not include the results from the findstr, but also an indication of the tring that was searched for as well as where it found it.

Any ideas ?... !


Report •

#3
June 13, 2012 at 20:48:41
You won't be able to do 'exactly' what you wanted. The /G option yields no
output regarding which item matched. Findstr will, but only the whole line,
not just the match-string. You need to leave off the /M, so that findstr will
deliver both the filename AND the matching line content. These are sep by a
colon. If this will work for you, then this might work:
for /f "tokens=1,2* delims=:" %%a in ('findstr /i /g:stringlisting.txt') do (
echo %%a >> test
echo %%b >> test
)
:end
Your output looks like:
file1
this-string1-matched
file2
this=string2-match

These data are available, formating is up to you...


Report •

Related Solutions

#4
June 14, 2012 at 04:05:54
✔ Best Answer
How about:

@ECHO OFF >X:\OutputFile.txt

FOR /F "tokens=*" %%A IN (X:\stringslisting.txt) DO (
    >>X:\OutputFile.txt ECHO Search String: "%%~A"
    >>X:\OutputFile.txt FINDSTR /S /I /M /C:"%%~A" "*.DOC"
    >>X:\OutputFile.txt ECHO(
)
EXIT /B

The above is less efficient since it has to open all the doc files multiple times (once for each search string), opposed to opening each doc file once and look for all search strings in the input text file, but I think it will provide what you are looking for!

When your only tool is a hammer, every problem looks like a nail.


Report •

#5
June 14, 2012 at 16:20:54
Thanks for the replies :-)

I'm trying them both now...


Report •

#6
June 14, 2012 at 20:49:56
@nbrane: Thanks for replying. I could not get your sugestion to work. Infact, it has been running for 3 hrs (about 2h55m longer than I would have expected!) on a fairly small data set.
So I think it has hung...

@orangeboy: hanks to you for your reply ! Yes, it still takes longer than I would have liked (and as you mentioned, it's cos it is running each one multiple times) but it produces the otput which is what i'm looking for.

eg ouput:
string
path\file

string
path\file

Thank you BOTH for replying, and orangeboy for the solution exactly what I was after :-)


Report •

#7
June 15, 2012 at 18:13:11
Water that has passed under the proverbial bridge department, Lol!: The only thing I can see is if the test file 'test' is in the path of the findstr, in which case it will keep finding, and adding to test, which would probably become "a very large file indeed!" Sorry my fix didn't fix, but i'm rusty from not having forum'd for many moons. If the testfile is placed somewhere out of the findstring orbit, maybe the problem will be resolved. Kudos to O-boy, nice save!

Report •

#8
June 18, 2012 at 17:08:48
Thanks to you both :)

Report •

Ask Question