Here is some sample xml:
<Transaction>
<Tender>
<TenderIdentifierSourceCode LanguageCode="eng" CodeContent="06"/>
</Tender>
I want to iterate through a directory and move all the xml files that contain the attribute CodeContent where the value is '06':
Tender/TenderIdentifierSourceCode/@CodeContent = '06'
So far, I have this which generates a file list (and just checks to see if the node exists) - But this doesn't work. And I want to check the VALUE of the attribute CodeContent rather than element existance:
$Path = "C:\Users\Bex\Documents\Client Projects\QA Refresh\Useful Powershell Scripts"
$PathArray = @()
$Results = "D:\MatchingStringResults.txt"
Get-ChildItem $Path -Filter "*.xml" -Recurse |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object { $NodeExists = $xml.Transaction.Tender.TenderIdentifierSourceCode
if($NodeExists){ $PathArray += $_.FullName
}
}
Write-Host "Contents of ArrayPath:" $PathArray | ForEach-Object {$_}
read-host "Script Complete - Press ENTER to exit"
$PathArray | % {$_} | Out-File $Results
答案 0 :(得分:0)
这样可行。它找到带有字符串的文件并移动到新目录。但是,正如PetSerAl提供的答案一样,我想检查特定的xml路径,因为CodeContent属性存在于多个元素中。
$Path = "C:\Users\Bex\Documents\Client Projects\QA Refresh\Useful Powershell Scripts\Apple Pay Transactions"
$Text = 'CodeContent="06"'
$PathArray = @()
$Results = "D:\MatchingStringResults.txt"
$Destination = "C:\Users\Bex\Documents\Client Projects\QA Refresh\Useful Powershell Scripts\FileList"
# This code snippet gets all the files in $Path that end in ".xml" and any subdirectories.
Get-ChildItem $Path -Filter "*.xml" -Recurse |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
Move-Item -path $_.FullName -destination $Destination
}
}