我正在制作一個批處理程式來顯示基于 .HTML 檔案的最新更新。我希望它準確地讀取“ 2022 年 10 月 26 日”部分(見下文),完全忽略其他代碼并將其放入變數中。
我想閱讀的部分檔案基于此:
<li><span>Current version:</span><span>**October 26, 2022**</span></li>
我會在這個批處理代碼中用!CurrentVersionDate 實作它!多變的
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 ::------------------------------------------------------------------
)
正如我所說,我希望它在沒有 HTML 代碼的情況下顯示,比如
::------------------------------------------------------------------
DLL File: "!file[%%i]!"
Version date: "!LastModifiedDate!"
Current version: October 26, 2022
::------------------------------------------------------------------
因此,我嘗試使用for /F "skip=385 delims=" %%i in (page.html) DO set "LastModifiedDate=%%i,它顯示以下結果:
Current version: Current version:</span> <span> October 26, 2022 </span></li>
我也試過FINDSTR /C:"Current version" page.html了,但沒用,因為它顯示了同樣的東西,而且我手中沒有變數。
有什么建議么?
uj5u.com熱心網友回復:
請參閱Using PowerShell and RegEx to extract text between delimiters,您可以嘗試使用這個使用 regex 的批處理示例:
@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
uj5u.com熱心網友回復:
:: %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
假設delayedexpansion生效,
對于檔案中的每一行,設定#line其內容,看...Current version...字串是否在行;如果是這樣,則洗掉該部分,然后使用結果,使用*和<作為分隔符來提取第一個剩余的標記并回傳它。
目前尚不清楚**示例中的 是文字還是試圖突出顯示所需的字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/526350.html
標籤:html批处理文件命令
上一篇:如何批量傳遞帶值的引數?
