简体   繁体   English

使用Powershell从位于不同文件夹中的xml文件创建逗号分隔的文本文件

[英]Using Powershell to create a comma delimited text file from xml files located in different folders

On a Windows 7 PC, I'm trying to generate a single comma-delimited text file from data contained in several hundred xml files. 在Windows 7 PC上,我试图从数百个xml文件中包含的数据生成一个逗号分隔的文本文件。 Each xml file is contained in its own uniquely-named folder, but each instance of the xml file has the same file name, "report.xml" 每个xml文件都包含在其自己的唯一命名文件夹中,但是xml文件的每个实例都具有相同的文件名“ report.xml”

To be more clear, the folder / file structure is something like this: 更清楚地说,文件夹/文件结构是这样的:

H:\Main_Folder\Folder_1\report.xml
H:\Main_Folder\Folder_2\report.xml
H:\Main_Folder\Folder_3\report.xml
...

The xml files contain lots of data, but I am only interested in a couple of data items, which happen to be measurements. xml文件包含大量数据,但是我只对几个数据项感兴趣,这些数据恰好是度量。 Ultimately, my desired output is a text file that would list, for every folder, the folder name and the two measurements from the xml file: 最终,我想要的输出是一个文本文件,该文件将为每个文件夹列出文件夹名称和xml文件中的两个度量:

folder_1 , measurement_1 , measurement_2
folder_2 , measurement_1 , measurement_2
folder_3 , measurement_1 , measurement_2
...

Within any folder, I am able to get the values I want from a single xml file with the following PowerShell code: 在任何文件夹中,我都可以使用以下PowerShell代码从单个xml文件中获取所需的值:

$xml = [xml](get-content report.xml)
$xml.Measurement[0].Value.'#text' + " , " + $xml.Measurement[1].Value.'#text'

The above code returns: 上面的代码返回:

measurement_1 , measurement_2

Also, I can move through each folder and output the folder name with the following PowerShell code: 另外,我可以遍历每个文件夹并使用以下PowerShell代码输出文件夹名称:

PS H:\Main_Folder> Get-ChildItem Report.xml -Recurse | Split-Path -parent

When executed, this gives me: 执行后,这给了我:

folder_1
folder_2
folder_3
...

I need some help to figure out how to get all of this to work together to produce my desired text file. 我需要一些帮助来弄清楚如何使所有这些协同工作以生成所需的文本文件。 Many thanks for your assistance. 非常感谢你的协助。

something like this 像这样的东西

ls h:\main_folder -filter "report.xml" -recurse|%{
    [xml]$xml=gc $_.FullName
    $_.DirectoryName+","+$xml.Measurement[0].Value.'#text' + " , " + $xml.Measurement[1].Value.'#text' |out-file c:\temp\output.csv -Append
}

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

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