Page 1 of 1
Batch file to read a file for specific words
Posted: 12 Feb 2014 03:44
by eshwarK
Hi Team,
I have requirements where an external processes call a batch file this batch file should read the log file and find for specific words like "Successfully","Failed","Aborted" and return a based on the values.
Like if it finds successfully then the batch script should value 1, for fialed/aborted it should be 2..
how to write the script or if you guys can provide links to sample script that can be used it will be great.
Thanks,
EshwarK
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 04:30
by foxidrive
Code: Select all
findstr /i "Successfully" "file.log" >nul && echo 1
findstr /i "Failed Aborted" "file.log" >nul && echo 2
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 04:36
by eshwarK
Thanks for the reply.
But will it return a value to a outside process which is calling batch file? If so can you please give me an example of this.
-EshwarK
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 05:00
by eshwarK
Thanks for the reply.
Will batch script returns a value to the calling process? if so can you please provide some sample.
thanks,
EshwarK
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 05:04
by foxidrive
eshwarK wrote:and return a based on the values.
the batch script should value 1, for fialed/aborted it should be 2..
Maybe you can explain what the above means (because it is missing some words) and how it is being used.
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 05:38
by eshwarK
foxidrive wrote:eshwarK wrote:and return a based on the values.
the batch script should value 1, for fialed/aborted it should be 2..
Maybe you can explain what the above means (because it is missing some words) and how it is being used.
Sorry for the confusion This is how the process will be:
1. External process call this batch file
2. batch file should read the log file and return a value based on the findstring (successfully/Failed/aborted)
3. External process should capture the value (external process has capability of capture the value)
Question is how can i return a value from batch file to the calling process?
Thanks,EshwarK
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 05:43
by foxidrive
eshwarK wrote:3. External process should capture the value (external process has capability of capture the value)
How does this process capture the value? In text? The snippet will return 1 or 2 as you asked.
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 11:27
by Aacini
Code: Select all
@echo off
set ret=0
findstr /i "Successfully" "file.log" >nul && set ret=1
findstr /i "Failed Aborted" "file.log" >nul && set ret=2
exit /B %ret%
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 21:18
by eshwarK
Aacini wrote:Code: Select all
@echo off
set ret=0
findstr /i "Successfully" "file.log" >nul && set ret=1
findstr /i "Failed Aborted" "file.log" >nul && set ret=2
exit /B %ret%
Thanks for the reply. How can i test this to make sure it is returning a value.
I have tried calling this bat file from another bat file but it is getting closed after the execution.
-EshwarK
Re: Batch file to read a file for specific words
Posted: 12 Feb 2014 21:42
by Aacini
test.bat:
Code: Select all
@echo off
call test2.bat
echo Value returned by test2.bat: %errorlevel%
test2.bat:
Code: Select all
@echo off
set ret=12345
exit /B %ret%
Output:
Code: Select all
C:\> test
Value returned by test2.bat: 12345
Antonio
Re: Batch file to read a file for specific words
Posted: 14 Feb 2014 01:05
by eshwarK
ECHO OFF
set return_code = 0
IF findstr /i "ERROR_OCCURRED" test.log > nul (
set return_code = 6
rem exit /B 0
echo %return_code%
) else (
IF findstr /i "WARNING_OCCURRED" test.log > nul (
set return_code = 4
rem exit /B 0
echo %return_code%
) else (
set return_code = 0
rem exit /B 0
echo %return_code%
)
)
pause
This is the code i was trying but the command prompt is getting closed without pause. I want to search for ERROR_OCCURRED string if found then return 6, else search for WARNING_OCCURRED then return 4 else 0.
Is This code correct? i want to see the result of this but the command prompt is getting closed. I will replace echo with exit /B after testing.
Thanks in advance,
Eshwark
using Findstring to find a string in a file.
Posted: 14 Feb 2014 02:12
by eshwarK
ECHO OFF
set return_code = 0
IF findstr /i "ERROR_OCCURRED" test.log > nul (
set return_code = 6
rem exit /B 0
echo %return_code%
) else (
IF findstr /i "WARNING_OCCURRED" test.log > nul (
set return_code = 4
rem exit /B 0
echo %return_code%
) else (
set return_code = 0
rem exit /B 0
echo %return_code%
)
)
pause
This is the code i was trying but the command prompt is getting closed without pause. I want to search for ERROR_OCCURRED string if found then return 6, else search for WARNING_OCCURRED then return 4 else 0.
Is This code correct? i want to see the result of this but the command prompt is getting closed. I will replace echo with exit /B after testing.
Thanks in advance,
Eshwark
Re: using Findstring to find a string in a file.
Posted: 14 Feb 2014 04:58
by carlos
try use this:
Code: Select all
@ECHO OFF
setlocal enabledelayedexpansion
set "return_code=0"
findstr /i "ERROR_OCCURRED" test.log > nul
IF not errorlevel 1 (
set "return_code=6"
rem exit /B 0
echo !return_code!
) else (
findstr /i "WARNING_OCCURRED" test.log > nul
IF not errorlevel 1 (
set "return_code=4"
rem exit /B 0
echo !return_code!
) else (
set "return_code=0"
rem exit /B 0
echo !return_code!
)
)
also, the logic can be simplified here to:
Code: Select all
@ECHO OFF
setlocal enabledelayedexpansion
set "return_code=0"
findstr /i "ERROR_OCCURRED" test.log > nul
IF not errorlevel 1 (
set "return_code=6"
) else (
findstr /i "WARNING_OCCURRED" test.log > nul
IF not errorlevel 1 (
set "return_code=4"
)
)
rem exit /B 0
echo %return_code%
Re: using Findstring to find a string in a file.
Posted: 14 Feb 2014 07:57
by Squashman
eshwarK wrote:IF findstr /i "ERROR_OCCURRED" test.log > nul (
You cannot chain commands together like that in batch like you can in other programming languages.