computing
  • 21

Solved search string & assign variables in .bat

  • 21

 

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

 

Share

0 Answers

  1. 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

    • 0