2

I have a .xml file that I need to edit with PowerShell. I need to delete the nodes that are older than 6 months. "File Id=" is the node that I need to search for. The File Id is a date and I need to delete anything older than 182 days.

Example .xml below....

<?xml version="1.0" encoding="utf-8"?>
<Files>
    <File Id="202304171534365167">
    <ZipFile>N563UW_563_170420230254_C8D5900000B0_0A281140</ZipFile>
    <Rds>CLT</Rds>
    <FileName>N563UW_563_170420230254_C8D5900000B0_0A281140.fdr.sfd</FileName>
    <Fleet>A32x1024</Fleet>
    <FileDate>17 Apr 2023 02:54</FileDate>
    <HoursofData>110.193</HoursofData>
    <AircraftID>563</AircraftID>
    <Status>1</Status>
  </File>
    <File Id="202101281622371208">
    <ZipFile>N521UW_521_280120210948_C8D590000031_0A281114</ZipFile>
    <Rds>CLT</Rds>
    <FileName>N521UW_521_280120210948_C8D590000031_0A281114.fdr.sfd</FileName>
    <Fleet>A32x_256</Fleet>
    <FileDate>28 Jan 2021 09:48</FileDate>
    <HoursofData>173.706</HoursofData>
    <AircraftID>521</AircraftID>
    <Status>1</Status>
  </File>
<File Id="202101280823338850">
    <ZipFile>N303RE_3RE_280120210224_C8D590000082_0A3C575B</ZipFile>
    <Rds>TUL_H12_1</Rds>
    <FileName>N303RE_3RE_280120210224_C8D590000082_0A3C575B.dlu.sfd</FileName>
    <Fleet>737-8</Fleet>
    <FileDate>28 Jan 2021 02:24</FileDate>
    <HoursofData>26.8256</HoursofData>
    <AircraftID>3RE</AircraftID>
    <Status>1</Status>
  </File>
</Files>

The code below will remove the File Id '202101281622371208' from the .xml file and saves it correctly, but I need it to find all the "File Id" nodes that are older than 182 days and delete them, so hard coding a specific date won't work. I can not wrap my head around how to do this. Any help greatly appreciated

# Need to delete entries older than 6 months
$XML_Date = Get-Date (Get-Date).AddDays(-182) -Format "yyyyMMddHHmmssms"

$xml = [xml](Get-Content D:\System\Index\Index.xml)
$node = $xml.SelectSingleNode("//File[@Id='202101281622371208']")
$node
$node.ParentNode.RemoveChild($node) | Out-Null
$xml.Save('D:\System\Index\Index.xml')
mklement0
  • 382,024
  • 64
  • 607
  • 775
mr_evans2u
  • 139
  • 1
  • 5
  • 15
  • You can use XSLT for the task: https://stackoverflow.com/questions/5544762/finding-the-difference-between-2-dates-in-xslt – Yitzhak Khabinsky Apr 26 '23 at 21:10
  • 1
    As an aside: `[xml] (Get-Content ...)` should be avoided, because `Get-Content` may misinterpret the character encoding of XML files. Use the `[xml]` type's `.Load()` method instead; e.g.: `($xmlDoc = [xml]::new()).Load((Convert-Path ./file.xml))` - see the bottom section of [this answer](https://stackoverflow.com/a/68087177/45375) for details. – mklement0 Apr 26 '23 at 21:19

1 Answers1

1

The following should work using PowerShell to perform the filtering, I'm not sure if you could use SelectNodes method to perform the filtering.

$daterange = [datetime]::Now.AddDays(-182) # You could use `.AddMonths(-6)` here too
($xml = [xml]::new()).Load('path\to\xmlfile.xml')
$xml.Files.File | Where-Object { [datetime] $_.FileDate -lt $daterange } |
    ForEach-Object { $null = $xml.Files.RemoveChild($_) }
$xml.Save('path\to\xmlfile.xml')
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37