简体   繁体   中英

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. 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"

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. 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:

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:

$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:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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