-2

I have an application that outputs XML files during the life of an event in the software. I need to parse the XML files and once certain text are matched, move the file to another folder

The file name format of each XML file is: Event Owner (MEMS), Year (2020) and serial number (00012345). The software will write same file multiple time throughout the event so I need to look for specific items before taking any action. I would prefer to do this in PowerShell if at all possible.

I need to find

 `<EventUnits>
      <Unit>
        <UnitCode>xxxxxxx></UnitCode>
</EventUnits>`

and

<EventDetails>
        <EventCompletedTime>YYYY-MM-DD HH:MM:SS</EventCompletedTime>
</EventDetails>

Once UnitCode equals one of 4 unit codes and EventCompleted has a valid date and time, the file is moved to a different folder.

1
  • 2
    Does this answer your question? Parsing xml using powershell There are a lot of posts that walks you through parsing xml in powershell. Commented Feb 24, 2020 at 23:09

1 Answer 1

0

You can read in the the file contents, telling powershell that you expect the file to be xml on ingestion. Then you can just access the objects within. For example:

# read in the file contents of xml file
[xml]$fileContents = Get-Content 'C:\my\file\path.xml'

# you should now be able to access the contents of the file using dot notation
Write-Host $fileContents.EventUnits.Unit.UnitCode
Write-Host $fileContents.EventDetails.EventCompletedTime

This example assumes that

  1. EventUnits and EventDetails are top-level nodes (not nested inside another xml node)
  2. The file would absolutely be there. If you are not sure you can test for the existence of the file
  3. The file will absolutely be in xml format
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.