我需要复制一个应用程序的存档文件(它们都是XML),然后使用“客户ID”和“采购订单号”重命名这些文件。为了找到这些值,我需要拆分元素标签上的数据以找到索引值,然后根据每个索引获取所需的值。几天前,当我完成代码时,它按预期工作。第二天,尝试再次运行它,并使用相同的名称重命名了所有文件。当我验证代码时(通过添加“假设”或“写主机”),我可以看到唯一的文件名。我试图使其重新工作,但无济于事。如果需要,我有示例XML文档。
这是代码(曾经工作过):
ForEach ($f in Get-ChildItem -path "C:\Archive\XML_Processed\Inbound\945-WHS_ShipAdvice_SW\" -Filter IB_945*.xml | Sort-Object)
{
$indxPatt1 = '<e03_0373>' ##Split Attribute/Element Tag in XML
$indx1 = (Get-Content $f.fullname) | Select-String -Pattern $indxPatt1 | ForEach-Object {$_.LineNumber} | Select-Object
$var1A = (Get-Content $f.fullname) | Select -Index $indx1
$patt1A = '<e04_0145>'
$patt2A = '</e04_0145>'
$var2A = (Get-Content $f.fullname) | Select-String -Pattern $var1A | Foreach-Object {$_ -replace $patt1A, ""}
$var2B = $var2A | Foreach-Object {$_ -replace $patt2A, ""}
$var3C1 = $var2B | Foreach-Object {$_ -replace " {1,30}", ""}
$var4A = $var3C1 | Foreach-Object {$_ -replace "^", "OrdNo_"}
$varTP1B = (Get-Content $f.fullname) | Select -Index 5
$pattTP1B = '<TradingPartner>'
$pattTP2B = '</TradingPartner>'
$varTP2B = (Get-Content $f.fullname) | Select-String -Pattern $varTP1B | Foreach-Object {$_ -replace $pattTP1B, ""}
$varTP3B = $varTP2B | Foreach-Object {$_ -replace $pattTP2B, ""}
$var3TPD1 = $varTP3B | Foreach-Object {$_ -replace " {1,30}", ""}
$varTP3D2 = $var3TPD1 | Foreach-Object {$_ -replace "^", "CustID_"}
$varNewName = $varTP3D2 + "_" + $var4A ##| Write-Host}
Get-ChildItem C:\Archive\XML_Processed\Inbound\945-WHS_ShipAdvice_SW -Filter *.xml | rename-item -newname { $_.Name -replace 'IB_945', $varNewName}; ##-WhatIf;
}
答案 0 :(得分:0)
如果我了解您要正确执行的操作(如果没有输入数据,这很难做到),那么(可能)您的最后一行应更改为:
$f | Rename-Item -Newname { $f.Name -replace 'IB_945', $varNewName} -WhatIf