Search code examples
arraysbatch-filesmtpstring-concatenation

enabledelayedexpansion concat string every time come in block batch script


I have below code which print(else code) error in command prompt when control come into it.But I want a string which contains all error in one string (Concat previous Strings), and use that string to send mail.

  @echo off
echo Working !!!
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type test.log ^| findstr "Queue_ID"') do (
   set $Line=%%a
   for /f "tokens=2 delims=^=," %%b in ('echo !$Line!') do (
      set $Value=%%b
      set $Value=#Queue_id!$Value: =!
      if not defined !$Value! (set !$Value!=1) else (echo Error with [!$Value!]) 
))
endlocal
echo Done !!!
Pause

and mail sending code is

S:\blat.exe -body " error_String came from else code" -subject "KSD Error" -tf %recipients% -server %smtpserver% -f %sender%

Solution

  • This will send an E-mail ONLY if there is some error :

      @echo off
    echo Working !!!
    setlocal enabledelayedexpansion
    for /f "delims=" %%a in ('type test.log ^| findstr "Queue_ID"') do (
       set $Line=%%a
       for /f "tokens=2 delims=^=," %%b in ('echo !$Line!') do (
          set $Value=%%b
          set $Value=#Queue_id!$Value: =!
          if not defined !$Value! (set !$Value!=1) else (set $liste=!$Liste!,[!$Value!]) 
    ))
    
    if defined $Liste (
      echo Error List : !$Liste!
      echo sending Mail...
      S:\blat.exe -body !$Liste! -subject "KSD Error" -tf %recipients% -server %smtpserver% -f %sender%)
    endlocal
    echo Done !!!
    Pause