1

I am trying to use find command with if..else statement. What I actually want to do is, search for a specific string in multiple log files. If I find that string I want to append the name of that file with "Passed" else "Failed" into another text file.

I have tried the following but it doesn't work:

if (find /n /i "0 error" "filename.log") (
    echo "Passed" > log.txt
) else (
    echo "Failed" > log.txt
)
2
  • 1
    Just to be clear, do you want to do this in the normal command line or through Powershell? You have tagged as both, but your comment says using CMD. Commented Jul 24, 2018 at 7:30
  • 1
    sorry, edited it. I want to use CMD Commented Jul 24, 2018 at 7:31

2 Answers 2

2

CMD/batch doesn't support commands in if statements. You need to evaluate the errorlevel (the exit code of the command) after running the command:

set "logfile=filename.log"
find /n /i "0 error" "%logfile%" >nul
if %errorlevel% equ 0 (
    >>log.txt echo %logfile% - Passed
) else (
    >>log.txt echo %logfile% - Failed
)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but it is not working in case of fail , and I will be using this for multiple files, so can this be modified more. Also, it doesn't append the name of file with that. I need to run a batch file for multiple files at once that can do this task.
@AnujMittal "it is not working" is not a problem description. What behavior do you observe? What behavior do you expect? What filename do you want to append, and where?
It says "Passed" even after the string "0 error" was not present. In the log.txt file created It only says "Passed", what i need to have is "Filename - Passed". In case of my multiple files scenario, I want if that exact string is present is should say passed else failed for eg: the sring is "0 Error" so the output should be in a log.txt file with "Filename1 - Passes Filename2- Passed Filename3- Failed" and so on till all the files in a folder are read.
then just store the file name in a variable and print the variable instead
I am very new to working with cmd, can you please give an example how to do it.
|
1

If I understand your question correctly:

To get a .txt file containing only the names of the logs in the current directory which contained 0 error:

FindStr /MIC:"0 Error" *.log>"Passed.txt"

If you wanted a log.txt to prodvide feedback on each log file in the current directory, instead…

From the Command Prompt:

(For %A In (*.log) Do @FindStr /MIC:"0 Error" "%A">Nul&&(@Echo %A Passed)||@Echo %A Failed)>"log.txt"

From a batch file:

@Echo Off
(For %%A In (*.log) Do FindStr /MIC:"0 Error" "%%A">Nul&&(
    Echo %%A Passed)||Echo %%A Failed)>"log.txt"

3 Comments

This was totally what I wanted, Thanks for the solution, just one thing. It is writing the whole content in log.txt file eg if it found the string then the result in log .txt is : C:\CODEDUI_TestAutomation\Models\MSBuildData\MSBuldLog>FindStr /MIC:"0 Error" "LocalMSBuild_Build_bd_CLR_Cmd and SettingFile1.log" 1>Nul && (Echo LocalMSBuild_Build_bd_CLR_Cmd and SettingFile1.log Passed ) || Echo LocalMSBuild_Build_bd_CLR_Cmd and SettingFile1.log Failed LocalMSBuild_Build_bd_CLR_Cmd and SettingFile1.log Passed
Can It have just the last line written in the file?
@AnujMittal, when I said existing batch file, I assumed you already had @Echo Off at some line above it, as I did in your provided code! I've updated my example.

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.