Batch file to read a file for specific words

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eshwarK
Posts: 7
Joined: 12 Feb 2014 03:37

Batch file to read a file for specific words

#1 Post by eshwarK » 12 Feb 2014 03:44

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to read a file for specific words

#2 Post by foxidrive » 12 Feb 2014 04:30

Code: Select all

findstr /i "Successfully" "file.log" >nul && echo 1
findstr /i "Failed Aborted" "file.log" >nul && echo 2

eshwarK
Posts: 7
Joined: 12 Feb 2014 03:37

Re: Batch file to read a file for specific words

#3 Post by eshwarK » 12 Feb 2014 04:36

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

eshwarK
Posts: 7
Joined: 12 Feb 2014 03:37

Re: Batch file to read a file for specific words

#4 Post by eshwarK » 12 Feb 2014 05:00

Thanks for the reply.

Will batch script returns a value to the calling process? if so can you please provide some sample.

thanks,
EshwarK

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to read a file for specific words

#5 Post by foxidrive » 12 Feb 2014 05:04

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.

eshwarK
Posts: 7
Joined: 12 Feb 2014 03:37

Re: Batch file to read a file for specific words

#6 Post by eshwarK » 12 Feb 2014 05:38

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to read a file for specific words

#7 Post by foxidrive » 12 Feb 2014 05:43

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.

Aacini
Expert
Posts: 1923
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch file to read a file for specific words

#8 Post by Aacini » 12 Feb 2014 11:27

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%

eshwarK
Posts: 7
Joined: 12 Feb 2014 03:37

Re: Batch file to read a file for specific words

#9 Post by eshwarK » 12 Feb 2014 21:18

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

Aacini
Expert
Posts: 1923
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch file to read a file for specific words

#10 Post by Aacini » 12 Feb 2014 21:42

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

eshwarK
Posts: 7
Joined: 12 Feb 2014 03:37

Re: Batch file to read a file for specific words

#11 Post by eshwarK » 14 Feb 2014 01:05

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

eshwarK
Posts: 7
Joined: 12 Feb 2014 03:37

using Findstring to find a string in a file.

#12 Post by eshwarK » 14 Feb 2014 02:12

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

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: using Findstring to find a string in a file.

#13 Post by carlos » 14 Feb 2014 04:58

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%


Squashman
Expert
Posts: 4487
Joined: 23 Dec 2011 13:59

Re: using Findstring to find a string in a file.

#14 Post by Squashman » 14 Feb 2014 07:57

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.

Post Reply