无法使用for循环从Windows批处理脚本编写html

前端之家收集整理的这篇文章主要介绍了无法使用for循环从Windows批处理脚本编写html前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
经过2天的努力,我仍然无法找到解决我的问题的 Windows批处理脚本.

我想要做的是逐行读取html文件,如果匹配关键字在特定行中找到,那么用某些东西替换该行(html标签和变量组合)

无论我做什么,每当我尝试将html标签推送到文件时,我总会得到一些错误“<当时是预期的”.看起来批处理脚本不喜欢html. 这是我的代码: script.bat

for /F "tokens=1,2,3,4,5,6,7" %%i in (output.txt) do call :process %%i %%j %%k %%l %%m %%n %%o
goto :sendreport

:: procedure to 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 to find in the 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 to look in

    FINDSTR %FINDWHAT% %FILE% >nul
    IF %ERRORLEVEL% EQU 1 GOTO nowork

    MOVE /Y "%FILE%" "%FILE%.bak"
    FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%FINDWHAT%"`) DO (
      SET LINE=%%A
    )
    FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S  ::read file line by line
        SET /A LINE=%LINE:~1,6%
        SET /A COUNT=1
        FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
          IF "!COUNT!" NEQ "%LINE%" (
              ECHO %%A>>"%FILE%" :: if the matching string not found,write the line as it is
          ) ELSE (
              ECHO %REPLACEWITH%>>"%FILE%" :: if found,replace the entire line with 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
for /F "tokens=1,7" %%i in (output.txt) do call :process %%i %%j %%k %%l %%m %%n %%o
goto :sendreport

:: procedure to 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 to find in the 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 to look in
    SET FILE=template.html

    FINDSTR %FINDWHAT% %FILE% >nul
    IF %ERRORLEVEL% EQU 1 GOTO nowork

    MOVE /Y "%FILE%" "%FILE%.bak" >nul
    FOR /F "delims=" %%A IN ('TYPE "%FILE%.bak"') DO (
     ECHO "%%A"|FIND /i "%findwhat%" >NUL
     IF ERRORLEVEL 1 (>>"%file%" echo %%A
     ) else (
      >>"%file%" echo !replacewith!
     )
    )
GOTO :eof

:sendreport
echo "done"
GOTO :eof

这应该可以胜任 – 如果我了解你试图通过四处走动来做什么.最好说这个问题不是要求修复无效的治疗方法.

>逐行抓取文件.
>查找目标字符串
>如果没有找到,只需复制线
>如果发现,吐出更换线.

诀窍是使用延迟扩展来防止替换线被解释.使用!var!,解析器不知道变量包含重定向,因此它不反对.在执行时,解析器已经完成了它的工作.

原文链接:https://www.f2er.com/windows/441247.html

猜你在找的Windows相关文章