我正在嘗試使用批處理腳本決議 XML 檔案中的一些資訊,但我無法弄清楚如何重命名變數并保持資料完整。輸出檔案是 HTML,這是讀取資料的設備所需要的。
批處理檔案可以很好地創建 HTML 檔案。設備正在尋找溫度:75,這就是檔案中的樣子,但是如果您使用 HTML 編輯器檢查檔案,則搜索名稱包含在變數中,并且設備無法使用該資料。
這就是我一直在做的事情;
@echo off
start /b /WAIT %~dp0bitsadmin.exe /transfer "currentstats"
https://w1.weather.gov/xml/current_obs/KMGM.xml %~dp0Weather.xml
for %%a in (%~dp0Weather.xml) do (
for /f "tokens=* " %%b in ( ' type "%%a" ^|findstr /i "temp_f" ' ) do set tem1="%%b"
for /f "tokens=* " %%b in ( ' type "%%a" ^|findstr /i "relative_humidity" ' ) do set
hum1="%%b"
)
for /f "tokens=* delims=" %%P in ("<HTML>") do ( @echo %%P > %~dp0Weather.html)
for /f "tokens=* delims=" %%P in (%tem1%) do ( @echo Temperature: %%P >> %~dp0Weather.html)
for /f "tokens=* delims=" %%P in ("<BR />") do ( @echo %%P >> %~dp0Weather.html)
for /f "tokens=* delims=" %%P in (%hum1%) do ( @echo Humidity: %%P >> %~dp0Weather.html)
for /f "tokens=* delims=" %%P in ("</HTML>") do ( @echo %%P >> %~dp0Weather.html)
資料樣本(來自評論)假定換行符。
<HTML> Temperature: <temp_f>75.0</temp_f> <BR />
Humidity: <relative_humidity>62</relative_humidity>
</HTML>
我不知道如何洗掉左右指向單角引號以及它們之間的內容,所以我最終得到了這個。
<HTML> Temperature: 75.0 <BR />
Humidity: 62
</HTML>
uj5u.com熱心網友回復:
我會考慮使用 PowerShell 來代替。使用 PowerShell,您應該能夠直接獲取資料,而不是下載和讀取 XML 檔案。它還使您有機會直接輸出到 HTML。
例子:
天氣資料.ps1
$weather_url = "https://w1.weather.gov/xml/current_obs/KMGM.xml"
$request = [System.Net.HttpWebRequest]::Create($weather_url)
$request.UserAgent = "MyApplication/v1.0 (http://foo.bar.baz; [email protected])"
$response = $request.GetResponse()
$doc = New-Object System.Xml.XmlDocument
$doc.Load($response.GetResponseStream())
$doc.current_observation | ConvertTo-HTML -As List @{Label = 'Temperature'; Expression = {[Int]$_.temp_f}}, @{Label = 'Humidity'; Expression = {$_.relative_humidity}} | Out-File ($MyInvocation.MyCommand.Path.DirectoryName 'weather.html')
要從批處理檔案運行它,請使用它,(顯然%UserProfile%\Desktop\
根據需要進行更改):
天氣測驗.cmd
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File "%UserProfile%\Desktop\WeatherData.ps1"
輸出檔案weather.html
應位于 cmd.exe 實體的當前目錄中。
注意:如果您想要十進制溫度,而不是我上面使用的整個整數,請更改[Int]$_.temp_f
為 just $_.temp_f
。
如果我選擇僅使用批處理檔案來執行此操作,那么我將使用內置curl.exe
實用程式,而不是bitsadmin.exe
:
天氣資料.cmd
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "_=" & For /F "Tokens=3 Delims=<>" %%G In ('%SystemRoot%\System32\curl.exe
"https://w1.weather.gov/xml/current_obs/KMGM.xml" -H "Accept: Application/XML"
2^>NUL ^| %SystemRoot%\System32\findstr.exe /IL "<temp_f> <relative_humidity>"
') Do @(If Not Defined _ (Set /P "=<HTML>Temperature: %%G<BR />" 0<NUL
Set "_=.") Else Set /P "=Humidity: %%G</HTML>" 0<NUL & Echo(
) 1>>"%~dp0weather.html"
uj5u.com熱心網友回復:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74092241.txt"
SET "omit="^<temp_f^>" "^</temp_f^>" "^<relative_humidity^>" "^</relative_humidity^>""
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
SET "line=%%e"
FOR %%y IN (%omit%) DO SET "line=!line:%%~y=!"
echo !line!
)
GOTO :EOF
>|<我相信關鍵是用脫字符號“escape”(關閉特殊含義)的^
uj5u.com熱心網友回復:
定義要保留的資料比定義要洗掉的資料要簡單得多!只需這樣做:
@echo off
setlocal EnableDelayedExpansion
rem Define keep values:
set "HTML=<HTML>"
set "BR /=<BR />"
set "/HTML=</HTML>"
(for /F "delims=" %%a in (Weather.xml) do (
set "line=%%a"
set "line=!line:<=%%!"
call set "line=!line:>=%%!"
echo !line!
)) > Weather.html
更改任何<name>
by%name%
并評估結果行。所有%varNames%
不存在的都將被洗掉...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517990.html
標籤:批处理文件
下一篇:簡單的關機腳本