简体   繁体   中英

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

I require to cut or move XML files based on the nodes, etc I want to search through the file pick up EG FX and move the files containing tfx to a different location

I've tried the following with no success

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

no result above does not run

You're most of the wasy there (I think, I need a sample of your XML file to be really sure), I think you're getting hung up here

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

With this line of code, you're saying to move all XML files, and I think you should just be referencing the current file within your loop. Here it is (rewritten a tiny bit for an easier to debug ForEach loop).

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

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