Home > Mobile >  Display an specific HTML text in batch file
Display an specific HTML text in batch file

Time:11-02

I'm making a batch program to show recent updates based on a .HTML document. I wanted it to read exactly the "October 26, 2022" part (seen below), completely ignoring the other codes and put it in a variable.

Part of the document I want to read is based on this:

<li><span>Current version:</span><span>**October 26, 2022**</span></li>

I would implement it inside this batch code with the !CurrentVersionDate! variable

FOR /L %%i in (1,1,%count%) DO (
call :GetLastModifiedDate "!File[%%i]!" LastModifiedDate
    echo ::------------------------------------------------------------------
    echo DLL File: "!file[%%i]!"
    echo Version date: "!LastModifiedDate!"
    echo Current version: "!CurrentVersionDate!"
    echo ::------------------------------------------------------------------
)

As I said, I wanted it to be displayed without the HTML codes, like

::------------------------------------------------------------------
DLL File: "!file[%%i]!"
Version date: "!LastModifiedDate!"
Current version: October 26, 2022
::------------------------------------------------------------------

So, I tried to use for /F "skip=385 delims=" %%i in (page.html) DO set "LastModifiedDate=%%i, it shows the following result:

Current version: Current version:</span> <span>  October 26, 2022  </span></li>

I also tried FINDSTR /C:"Current version" page.html, but it was useless, because it showed the same thing and I didn't have the variable in my hands.

Any suggestions?

CodePudding user response:

Refer to Using PowerShell and RegEx to extract text between delimiters, You can give a try with this example :


@echo off
Title Using PowerShell and RegEx to extract text between delimiters
@for /f "tokens=*delims=" %%a in ('Type page.html ^| FINDSTR /I /C:"Current Version"') do (
    @for /f "delims=" %%D in (
        'Powershell -C "[regex]::Matches('%%a','((?<=\<span\>). ?(?=<\/span>))').Groups[2].Value.Trim()"'
        ) Do (
            Set "CurrentVersion=%%D"
        )
)
echo Current Version : %CurrentVersion%
pause

CodePudding user response:

:: %1=quoted filename
:: %2=variablename to return value
:lastmodifieddate
for /f "usebackqdelims=" %%e in (%1) do (
 set "#line=%%e"
 if "!#line!" neq "!#line:<li><span>Current version:</span><span>=!" (
  set "#line=!#line:*<li><span>Current version:</span><span>=!"
  for /f "delims=*<" %%y in ("!#line!") do set "%2=%%y"&goto :eof
 )
)
goto :eof

Assuming delayedexpansion is in effect,

For each line in the file, set #line to its content, see whether the ...Current version... string is on the line; if so, delete that portion, then use the result, with * and < as delimiters to extract the first remaining token and return it.

It's not clear whether the ** in the sample is literal or an attempt to highlight the required string.

  • Related