我對腳本很陌生,我一直在解決這個問題。
我想提取一個 zip 檔案,在該 zip 檔案中有一個名為 file.xml 的 xml 檔案,我需要從中提取資訊。
該檔案很大,我只需要提取兩個標簽之間的資訊。
<Report name="result\_many fail" summary="20" yes=19 no="1" finished=20>
我需要提取標簽名稱和完成之間的資訊并將其保存到 txt 檔案中。txt 檔案應如下所示:
name result_many fail, summary 20, yes 19 , no 1, finished 20
問題是它解壓縮到正確的目標檔案夾,但它沒有將任何內容保存到 result.txt 檔案中。我的txt檔案總是空的。
這是我的代碼:
echo "unzipping file..."
powershell "Expand-Archive C:\path_to_zip_file -Destinationpath C:\path_to_to_destination_folder;
$Path = “C:\path_to_to_destination_folder\file.xml;”
Select-Xml -XPath '//Report[@finished]').Node.InnerText;
Set-Content -Path 'C:\path_to_to_destination_folder\result.txt'
@echo "done"
有人可以幫我嗎?
uj5u.com熱心網友回復:
撇開您的問題的附帶方面(似乎cmd.exe是通過嘗試傳遞多行命令字串,無效的示例 XML,缺少 檔案引數Select-Xml,缺少Set-Contentcmdlet的輸入來呼叫 from ):
$path = 'C:\path_to_to_destination_folder\file.xml'
(Select-Xml '//Report[@finished]' $path).
Node.Attributes.ForEach({ $_.Name ' ' $_.Value }) -join ', ' |
Set-Content C:\path_to_to_destination_folder\result.txt
要從(批處理檔案)呼叫 PowerShell 代碼,cmd.exe您需要根據本答案中描述的技術,將其在單行上傳遞,或者使用 將其小心地分布在多行中,并帶有續行:^
@echo off
echo unzipping file...
powershell -c Expand-Archive C:\path_to_zip_file -DestinationPath C:\path_to_to_destination_folder; ^
$path = 'C:\path_to_to_destination_folder\file.xml'; ^
(Select-Xml '//Report[@finished]' $path). ^
Node.Attributes.ForEach({ $_.Name ' ' $_.Value }) -join ', ' ^| ^
Set-Content C:\path_to_to_destination_folder\result.txt
echo done
如果您需要基礎知識方面的幫助,這里有一些PowerShell 學習資源:
一個起點是學習 PowerShell。
盡管本系列文章早于 PowerShell (Core) 6 ,但它是對 PowerShell 的出色的、面向配方的介紹。
http://hyperpolyglot.org/shell并列POSIX兼容殼的語法如
bash與的cmd.exe和PowerShell在簡潔,表格形式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/328042.html
標籤:电源外壳
