Solved search string & assign variables in .bat

May 22, 2012 at 15:19:43
Specs: Windows 2003
Hi,


I have an output file which may contain 3 different messages (i.e. "successfully loaded/unloaded", "Errors", & "Failed"). I want my .bat script to be able to search these messages through the output file and assign variable for each of the messages. Here are my requirements:

If the file contains only "successfully loaded/unload", set %status%=="Success"
If the file contains both "successfully loaded/unload" && "Errors", set %status%=="Error".
If the file contains "Failed", set %status%=="Fails"


echo on & setlocal EnableDelayedExpansion
set status=fails
set file=C:\testfile.txt

for /f "token=*" %%a IN %file% DO
(
findstr /c /i "successfully loaded/unloaded" %%a && findstr /c /i "Error" %%a && findstr /c /i "Failed" %%a
(IF %%a=="successfully loaded/unloaded" && %%a!="Error" (set %status%==Success echo %%a)
ELSE IF %%a=="successfully loaded/unloaded" && %%a=="Error" (set %status%==Error echo %status%)
ELSE set %status%=="Fails" echo %status%
)
)


when I run the script, it's never entered into the for loop, Could someone please help w/ my logic & syntax?

Thanks.
Daigia


See More: search string & assign variables in .bat

Report •


#1
May 22, 2012 at 18:22:25
✔ Best Answer
From your explanation, I don't see a need to use a loop. Maybe I'm missing something?


@echo off
findstr /c /i "Failed" %file%
if %errorlevel% equ 0 goto fail
findstr /c /i "Errors" %file%
if %errorlevel% equ 0 goto error
goto success

:fail

:: Do whatever you like here


:error

:: Do whatever you like here

:success

:: Do whatever you like here


Report •

#2
May 23, 2012 at 10:23:02
Actually, it doesn't need to be in the loop as long as it serves the purpose, it's fine. Basically, if it finds the %status% eq errors or failed, it should set the status to error or fails respectively.

I follow your suggestion, but it's still fails w/ "failed" & "errors" scenarios.

Here is my script.

echo on & setlocal EnableDelayedExpansion
set file=C:\testfile.txt

findstr /i /c "Failed" %file%
if %errorlevel% == 0 goto fail

findstr /i /c "Error" %file%
%errorlevel% == 0 goto error
goto success

:fail
set status=fail
echo "I am at %status% stage"

:error
set status=error
echo "I am at %status% stage"

:success
set status=success
echo "I am at %status% stage"

Here is the output of the script & don't worry about echo on (debugging purposes):


C:\>Script1.bat

C:\>echo on & setlocal EnableDelayedExpansion

C:\>set file=C:\testfile.txt

C:\>findstr /i /c "Failed" C:\testfile.txt
FINDSTR: /c ignored

C:\>if 1 == 0 goto fail

C:\>findstr /i /c "Error" C:\testfile.txt
FINDSTR: /c ignored
Error is here
errors
error ....

C:\>0 == 0 goto error
'0' is not recognized as an internal or external command, operable program or batch file.

C:\>goto success

C:\>set status=success

C:\>echo "I am at success stage"
"I am at success stage"

I really appreciate your helps.
Thanks in advance,
-Daigia


Report •

#3
May 23, 2012 at 10:59:33
Also the reason for the loop is that if it finds "fails" message, it will not do any more searching, get out of the loop, & set the %status%=fails. Basically I only want 1 %status% code at the end.

I test your suggestion w/ 3 messages: "successfully loaded", "errors", & "fails", it displays all 3 :-(


Report •

Related Solutions

#4
May 23, 2012 at 14:44:37
figure it out. Problem solved. All I need is to add "goto here" at the end of each function (:fails, :errors, :success) and add :here function for the next lines of codes.

echo off & setlocal EnableDelayedExpansion
set file=C:\testfile.txt

findstr /i /c "Failed" %file%
if %errorlevel% == 0 goto fails

findstr /i /c "Error" %file%
if %errorlevel% == 0 goto errors
goto success

:fails
set status=fails
goto here

:errors
set status=errors
goto here

:success
set status=success

:here
:: execute the next lines of codes.


Report •

#5
May 24, 2012 at 03:29:51
I do not see a /C switch in FINDSTR. I do see /C: - isn't that what you want to use?

When your only tool is a hammer, every problem looks like a nail.


Report •

#6
May 25, 2012 at 09:19:07
Thx Orangeboy for pointing out.

Report •

Ask Question