【问题标题】:Bulk rename XML files based on node value根据节点值批量重命名 XML 文件
【发布时间】:2019-09-27 00:43:30
【问题描述】:

我一直在尝试准备一个 powershell 脚本,它将所有 XML 文件重命名为 XML 节点之一的值:

$items = Get-ChildItem "C:\test\" -Filter *.xml
foreach ($item in $items) {
    $x = New-Object -TypeName 'System.XML.XMLDocument'
    $x.Load($item.FullName)
    $OutputFileName = $x.SelectSingleNode("//CreationDate").InnerText
    $OutputFileName = $OutputFileName -replace "-", "_"
    $OutputFileName = $OutputFileName -replace ":", "_"
    $OutputFileName = $OutputFileName -replace "T", "_"
    Rename-Item $item.Fullname -NewName "$OutputFileName.xml"
}

我遇到的问题是$x 总是空白,似乎我无法正确提取 XML 节点的值。

XML 结构(请记住<ActualStartDate> 节点在文件之间更改,其余名称相同)

<?xml version="1.0" encoding="utf-8"?>
<Request xmlns="link">
    <ActualStartDate>
        <TypeCode>0400</TypeCode>
        <VerNumber>2</VerNumber>
        <CreationDate>2012-01-31T14:33:35</CreationDate>
    </ActualStartDate>
</Request>

【问题讨论】:

  • 你能发布你的xml文件结构吗
  • 我写了这样的东西,并且可以从 Xml 读取数据。 $items = Get-ChildItem "D:\TESTBI" -Filter *.xml foreach($item in $item) { Write-Output $item.FullName $x = New-Object -TypeName XML $x.Load($item. FullName) $OutputFileName = $x.SelectSingleNode("//Connection").InnerText 写入输出 $OutputFileName }
  • @Roshan XML 示例结构已添加,抱歉,首先应该这样做
  • 你还没有关闭 Typecode 标签
  • 我没有看到代码有任何问题。只需检查 XML 标签。由于您正在搜索 CreationDate 节点在哪里并不重要。它直接搜索CreationDate的第一个节点。

标签: xml powershell rename


【解决方案1】:

您的代码适用于给定的示例 XML,但您遇到的问题可能来自使用 XPath 区分大小写这一事实。也许标签CreationDate在其他一些文件中被称为creationDate,或者它可能根本不存在。

也许试试这个:

Get-ChildItem 'C:\test' -Filter *.xml -File | ForEach-Object {
    [xml]$x = $_ | Get-Content -Encoding UTF8

    # get the value of tag CreationDate using XPath (case Sensitive)
    # $OutputFileName = $x.SelectSingleNode("//CreationDate").InnerText
    # or by following the properties of the xml object (case-Insensitive)
    $outputFileName = @($x.Request.ActualStartDate.CreationDate)[0]

    if ([string]::IsNullOrWhiteSpace($outputFileName)) {
        Write-Warning "Could not find value for CreationDate in file '$($_.FullName)'" 
    }
    else {
        $OutputFileName = $OutputFileName -replace '[-:T]', '_'

        Write-Host "Renaming file $($_.FullName) to $OutputFileName.xml"
        $_ | Rename-Item -NewName "$OutputFileName.xml" -Force
    }
 }

【讨论】:

    【解决方案2】:

    非常感谢所有提供帮助的人。

    最后,我发现问题在于我在 XML 文件中有一个名称空间,其中一个链接不起作用。我没有将命名空间放入示例中,我不知道在那种情况下它很重要,对此感到抱歉。

    在脚本中添加额外的 2 行来忽略该命名空间定义为我解决了这个问题:

    $xml = New-Object xml
    $xml = ([String](Get-Content "file.xml") -replace 'xmlns.*CONFIG">','>' )
    

    (感谢:http://www.kv0.org/blog/2012/05/04/ignore-xml-namespaces-in-powershell/

    【讨论】:

      猜你喜欢
      • 2014-12-11
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 2018-09-30
      • 1970-01-01
      • 2013-12-19
      相关资源
      最近更新 更多