Unable to write html from windows batch script using for loop -


after struggling 2 days still have not been able find solution problem windows batch script.

what want read html file line line , if matching keyword found in particular line, replace line (html tags , variable combination)

no matter whatever do, error "< expected @ time" whenever try push html tags file. looks batch script not html.

here code:

script.bat

for /f "tokens=1,2,3,4,5,6,7" %%i in (output.txt) call :process %%i %%j %%k %%l %%m %%n %%o goto :sendreport  :: procedure prepare report  :process     setlocal enabledelayedexpansion      set ubename=%1     set ubever=%2     set ubestat=%3     set rundate=%4     set starttime=%5     set endtime=%6     set totaltime=%7      set findwhat=%ubename%%ubever%  :: letter find in file     set replacewith=^<tr^>^<td^> %ubename% ^</td^>^<td^> %ubever% ^</td^>^<td^> %ubestat% ^</td^>^<td^> %rundate% ^</td^>^<td^> %starttime% ^</td^>^<td^> %endtime% ^</td^>^<td^> %totaltime% ^</td^>^</tr^>     set file=template.html  :: file in      findstr %findwhat% %file% >nul     if %errorlevel% equ 1 goto nowork      move /y "%file%" "%file%.bak"     /f "usebackq tokens=*" %%a in (`type "%file%.bak" ^|find /n /i "%findwhat%"`) (       set line=%%a     )     /f "tokens=1,2* delims=]" %%s in ("%line%") set line=%%s  ::read file line line         set /a line=%line:~1,6%         set /a count=1         /f "usebackq tokens=*" %%a in (`find /v "" ^<"%file%.bak"`) (           if "!count!" neq "%line%" (               echo %%a>>"%file%" :: if matching string not found, write line           ) else (               echo %replacewith%>>"%file%" :: if found, replace entire line propval           )           set /a count+=1         )     goto end     :nowork      :end ) :sendreport echo "done" 

output.txt

r560359c    ba0001  done    113121  24046   113121  24047 r560902c    bas0006 done    113121  24647   113121  45726 r560902c    bas0005 done    113121  24647   113121  45155 r560902c    bas0009 done    113121  45754   113121  70022 

template.html

<html> <body> <table> <tr id='r560902cbas0009'><td>r093021</td><td>basj1tna</td><td>not_started</td><td>-</td><td>-</td><td>-</td><td>-</td></tr> <tr id='r6213g04ba0001'><td>r6213g04</td><td>ba0001</td><td>not_started</td><td>-</td><td>-</td><td>-</td><td>-</td></tr> <tr id='r560359bba0001'><td>r560359b</td><td>ba0001</td><td>not_started</td><td>-</td><td>-</td><td>-</td><td>-</td></tr> <tr id='r560902cbas0006'><td>r560902c</td><td>bas0006</td><td>not_started</td><td>-</td><td>-</td><td>-</td><td>-</td></tr> <tr id='r560902cbas0005'><td>r560902c</td><td>bas0005</td><td>not_started</td><td>-</td><td>-</td><td>-</td><td>-</td></tr> <tr id='r560359cba0001'><td>r560902c</td><td>bas0009</td><td>not_started</td><td>-</td><td>-</td><td>-</td><td>-</td></tr> </table> </body> </html> 

@echo off setlocal /f "tokens=1,2,3,4,5,6,7" %%i in (output.txt) call :process %%i %%j %%k %%l %%m %%n %%o goto :sendreport  :: procedure prepare report  :process     setlocal enabledelayedexpansion      set ubename=%1     set ubever=%2     set ubestat=%3     set rundate=%4     set starttime=%5     set endtime=%6     set totaltime=%7  :: letter find in file     set findwhat=%ubename%%ubever%     set replacewith=^<tr^>^<td^> %ubename% ^</td^>^<td^> %ubever% ^</td^>^<td^> %ubestat% ^</td^>^<td^> %rundate% ^</td^>^<td^> %starttime% ^</td^>^<td^> %endtime% ^</td^>^<td^> %totaltime% ^</td^>^</tr^> :: file in     set file=template.html      findstr %findwhat% %file% >nul     if %errorlevel% equ 1 goto nowork      move /y "%file%" "%file%.bak" >nul     /f "delims=" %%a in ('type "%file%.bak"') (      echo "%%a"|find /i "%findwhat%" >nul      if errorlevel 1 (>>"%file%" echo %%a      ) else (       >>"%file%" echo !replacewith!      )     ) goto :eof  :sendreport echo "done" goto :eof 

this should job - if understand you're attempting going around houses. best whay problem rather ask fix cure doesn't work.

  • grab file, line-by-line.
  • look target string
  • if not found, xerox line
  • if found, spit out replacement line.

the trick being use delayed-expansion prevent replacement line being interpreted. !var!, parser doesn't know variable contains redirects, doesn't object. @ execution time, parser's done job.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -