1

I'm currently doing a findstr from a set of log files called link.2011*.log using a batch file with a SET command but am having trouble trying to echo the log file.

Code:

...
set /P log=.\DIR\%DEV%\link.2011*.log
findstr /L /C:"matrix" %log%
if errorlevel 1 (
echo %DEV% --- matrix not found >> .\output.txt
) else (
echo %DEV% --- matrix found %log% >> .\output.txt
:END

The output.txt does print the %dev% variable but the %log% variable outputs the whole string .\DIR\%DEV%\link.2011*.log

I would like the code to output the actual link.2011xxxxxxxx.log rather than the string.

Any help is appreciated.

Thanks.

2
  • 1
    I don't understand what set /P log=.\DIR\%DEV%\link.2011*.log is supposed to do. On my system, that writes .\DIR\%DEV%\link.2011*.log to the console as a "prompt", and if the user types something in response, it gets saved as the variable %log%. Are there systems where it does something different?
    – ruakh
    Feb 7, 2012 at 18:57
  • @ruakh - my idea was to use the set command to pick each of the log file from the directory and use the %log% variable to findstr for a specific string. Not sure if this answer your question. If you're looking for a user interaction via command window then I think I may have seen it somewhere in stackoverflow, just search for it.
    – redsu
    Feb 8, 2012 at 16:07

1 Answer 1

0

If you use the /M switch with the FINDSTR command, the output will only display the name(s) of the file(s) where the match has taken place:

FINDSTR /M /L /C:"matrix" "%log%" >> .\output.txt

If you want the output to be exactly like in your example script, you could try the following approach:

FINDSTR /M /L /C:"matrix" "%log%" > .\tmpoutput.txt && (
  ECHO %DEV% --- matrix found: >> .\output.txt
) || (
  ECHO %DEV% --- matrix not found >> .\output.txt
)
TYPE .\tmpoutput.txt >> .\output.txt
DEL .\tmpoutput.txt

That is, the FINDSTR command searches for the string and outputs the list of files into a temporary file. If the search was successful, the matrix found message gets printed to .\output.txt, otherwise the matrix not found one does.

Afterwards, the contents of .\tmpoutput.txt is appended to output.txt. (You can have no worries about this step being done in case there was no match, because in that case the temporary file will be empty, so it will not affect the contents of output.txt.)

2
  • Back again, I got another question regarding the script. Say for example I have a list of *.logs within a .\TEMP*.logs which I want to findstr "matrix" per log. But I want the output.txt to show the string matrix if found and the name of the log it was found. Thanks again Andriy.
    – redsu
    Feb 8, 2012 at 14:52
  • @redshogun: Without /M, FINDSTR displays both the file names and the actual lines where the matches took place. The format is: filename:line. Just try the command separately to see it for yourself. (You will also see that the file names are highlighted as shown by FINDSTR itself, but the highlighting gets cancelled after redirecting the output.)
    – Andriy M
    Feb 8, 2012 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.