2
IF find tcp.client == 1 

(


 findstr tcp.client *_chkpackage.log>summary.txt 
) 

ELSE 
(

#write only "filename" & "N/A" >> summary.txt

)

i want to search a file that search some value in all text files in folder if found the line that contain information i need it is going to write that line to text fiel if not found the it will write only filename and "N/A" to the line

i know it wrong but my coding skill suck so i have to ask

thank you so much for the answer

2 Answers 2

4

Something that works according to your "specification" would be that one:

@echo off
if exist summary.txt del summary.txt
for %%f in (*_chkpackage.log) do (
  find "tcp.client" %%f>NUL:
  IF errorlevel 1 (
    echo %%f N/A >> summary.txt
  ) ELSE (
    findstr "tcp.client" %%f >> summary.txt
  )
)
type summary.txt

Note that the if errorlevel 1 means "if errorlevel is greater or equal 1", for that I swapped your comparison, because if errorlevel 0 is true even when errorlevel equals 1.

Sign up to request clarification or add additional context in comments.

4 Comments

Your success output is missing the filename prefix. Also, FINDSTR sets its own errorlevel, so no need for the extra FIND step.
Hmm. In the success case there was no filename-prefix requested. So the extra find was intruduced to handle these different cases of output :-)
It was not explicitly requested, but the OP code uses FINDSTR with a wild card in the filespec, which results in the file name being prefixed to each matching line. I'm quite sure the OP would like to keep that functionality. You removed the wildcard, which removes the name prefix from the output.
Look at my answer for a concise way to sove the problem :)
2

I'm assuming the dot is a dot and not a regex wildcard, in which case you need to tell FINDSTR to do a literal search.

FINDSTR returns success if found, error if not found. The || operator conditionally executes commands if the prior command failed.

Use FOR loop to get each file individually, and add an additional file (nul) that will never match to force inclusion of file name in output.

Enclose entire construct in parentheses and redirect the entire block

@echo off
>summary.txt (
  for %%F in (*_chkpackage.log) do findstr /l "tcp.client" "%%F" nul||echo %%F N/A
)

2 Comments

@eathapeking - If you get a good post that fully answers your question and meets your needs then you should accept it by clicking on the check mark near the upper left corner. That action lets others know the question has been answered, it awards you 2 rep points, and awards the answer poster 15 points. Only one answer can be accepted per question. Once you reach 15 rep points you gain the privilege to up vote any answer you find useful, including answers to other people's questions. An up vote awards 10 points to the answer poster.
yep i did it :) but why it have to be only 1 check for the best answer T^T everyone should get mark for answer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.