使用Powershell将所有xml文件的内容写入文本文件

时间:2016-03-28 21:23:34

标签: powershell

Folder LogBackup包含40K xml文件。 我想读取每个xml文件并将其内容复制到不同的文本文件行。 例如,如果我有2个xml文件:

xmlFile1.xml

<?xml version = "1.0" encoding = "ISO-8859-1" ?><Acknowledgement><HeaderData><Description>Estimate</Description> <SiteQueueName>QUEUE</SiteQueueName><LineItems>7</LineItems><TimeStamp>201408310925444111</TimeStamp></HeaderData><OrderDetail>

xmlFile2.xml

<?xml version = "1.0" encoding = "ISO-8859-1" ?><Acknowledgement><HeaderData><Description>Estimate</Description> <SiteQueueName>QUEUE2</SiteQueueName><LineItems>7</LineItems><TimeStamp>201408310925380810</TimeStamp></HeaderData><OrderDetail>

这两个都应该复制到output.log:

<?xml version = "1.0" encoding = "ISO-8859-1" ?><Acknowledgement><HeaderData><Description>Estimate</Description> <SiteQueueName>QUEUE</SiteQueueName><LineItems>7</LineItems><TimeStamp>201408310925444111</TimeStamp></HeaderData><OrderDetail>
<?xml version = "1.0" encoding = "ISO-8859-1" ?><Acknowledgement><HeaderData><Description>Estimate</Description> <SiteQueueName>QUEUE2</SiteQueueName><LineItems>7</LineItems><TimeStamp>201408310925380810</TimeStamp></HeaderData><OrderDetail>

这是我到目前为止所得到的,但它不起作用:(

Get-ChildItem "C:\LogBackup" -Filter *.xml | 
Foreach-Object 
{
    $content = Get-Content $_.FullName
    #save content to a new file 
    $content | Set-Content ('output.log')
}

1 个答案:

答案 0 :(得分:1)

Set-Content

替换文件中的现有数据。

Add-Content

将数据附加到文件中。

因此,您只是将您检查的最后一个XML写入output.log文件,其余的将在此过程中被丢弃。

以下是Technet文章供参考。

...和修改后的代码片段(注意我现在将output.log转储到顶部):

Clear-Content ('C:\LogBackup\Output.log')
Get-ChildItem "C:\LogBackup" -Filter *.xml |
Foreach-Object 
{
   $content = Get-Content $_.FullName
   $content | Add-Content ('C:\LogBackup\Output.log')
}

退让

<?xml version = "1.0" encoding = "ISO-8859-1" ?><Acknowledgement><HeaderData><Description>Estimate</Description> <SiteQueueName>QUEUE</SiteQueueName><LineItems>7</LineItems><TimeStamp>201408310925444111</TimeStamp></HeaderData><OrderDetail>
<?xml version = "1.0" encoding = "ISO-8859-1" ?><Acknowledgement><HeaderData><Description>Estimate</Description> <SiteQueueName>QUEUE2</SiteQueueName><LineItems>7</LineItems><TimeStamp>201408310925380810</TimeStamp></HeaderData><OrderDetail>