簡體   English   中英

如何通過powershell,batchfile根據節點將XML文件剪切並將其移動到其他位置

[英]How to cut xml files and move them to a different location based on nodes via powershell , batchfile

我需要根據節點剪切或移動XML文件,等等。我想搜索整個文件,選擇EG FX,然后將包含tfx的文件移動到其他位置

我嘗試了以下失敗的嘗試

$dir = 'C:\temp\source'
Get-ChildItem -Path $dir -Filter *.xml | Foreach-Object {
  $Instrument = Select-Xml -Xpath '/deal' -Path $_.FullName  -ErrorAction SilentlyContinue
  If($Instrument.node.innertext -eq "FX_Cross"){
    Move-Item "C:\temp\source\*.xml" "C:\temp\destination\FX" -Force
  } Else{
    Move-Item "C:\temp\source\*.xml" "C:\temp\destination\" -Force
  }
}

上面沒有結果不運行

您在那兒很費力(我想,我確實需要您的XML文件樣本),我想您已經在這里掛了

Move-Item "C:\temp\source\*.xml" "C:\temp\destination\FX" -Force

使用這一行代碼,您要說的是移動所有XML文件,我想您應該只在循環中引用當前文件。 在這里(重寫了一點點以便於調試ForEach循環)。

$dir = 'C:\temp\source'
$xmlFiles = Get-ChildItem -Path $dir -Filter *xml 
ForEach($xmlFile in $xmlFiles){
    $Instrument = Select-Xml -Xpath '/deal' -Path $xmlFile.FullName -ErrorAction SilentlyContinue
    If($Instrument.node.innertext -eq "FX_Cross"){
        Move-Item -Path $xmlFile.FullName -Destination "C:\temp\destination\FX" -Force
    }
    Else{
        Move-Item -Path $xmlFile.FullName -Destination "C:\temp\destination\" -Force
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM