简体   繁体   English

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

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

I am pretty new to scripting and I have been stuck on this problem.我对脚本很陌生,我一直在解决这个问题。

I want to extract a zip file and in that zip file there is a xml file called file.xml which I need to extract information from.我想提取一个 zip 文件,在该 zip 文件中有一个名为 file.xml 的 xml 文件,我需要从中提取信息。

The file is huge and I only need to extract info between two tags.该文件很大,我只需要提取两个标签之间的信息。

<Report name="result\_many fail" summary="20" yes=19 no="1" finished=20> 

I need to extract information between the tags name and finished and save it to a txt file.我需要提取标签名称和完成之间的信息并将其保存到 txt 文件中。 The txt file should look like this: txt 文件应如下所示:

name result_many fail, summary 20, yes 19 , no  1, finished 20

The problem is that it unzips to the right destination folder but it doesn't save anything into the result.txt file.问题是它解压缩到正确的目标文件夹,但它没有将任何内容保存到 result.txt 文件中。 My txt file is always empty.我的txt文件总是空的。

This is my code:这是我的代码:

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"

Could someone help me out?有人可以帮我吗?

Leaving aside incidental aspects of your question (seemingly calling from cmd.exe , the broken attempt to extract a ZIP archive, lack of a file argument to Select-Xml , lack of input to the Set-Content cmdlet):撇开您的问题的附带方面(似乎是从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