繁体   English   中英

使用 powershell 提取 zip 文件,读取文件,然后提取标签之间的值

[英]extracting zip file with powershell , reading the file and then extracting value between tags

我对脚本很陌生,我一直在解决这个问题。

我想提取一个 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 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"

有人可以帮我吗?

撇开您的问题的附带方面(似乎是从cmd.exe调用,提取 ZIP 存档的失败尝试, Select-Xml缺少文件参数,缺少Set-Content cmdlet 的输入):

$file = 'C:\path_to_to_destination_folder\file.xml'
(Select-Xml '//Report[@finished]' $file).
  Node.Attributes.ForEach({ $_.Name + ' ' + $_.Value }) -join ', ' |
    Set-Content -Path C:\path_to_to_destination_folder\result.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM