I have 5 *.xml files in a directory. Within each file, there is a line: Content-ID:
The literal content id data is 3-4 delimited pieces. I need to replace the 1st **** with $date ( $date = Get-Date -format “yyyyMMdd” )
Your suggestions are greatly appreciated. Thank you!
On a high level you could do something like this:
Read all xml files you want to modify and store this list in a variable. Create a foreach loop over this variable. Inside this loop read the xml nodes of the file, select the node you want to change and use the -replace operator with a sofisticated regex pattern. Then you just have to save the changed xml data either to the original file or to a new one.
Thank you so much, Olaf. Here’s my biggest struggle: I don’t know what the text is that’s needing to be replaced.
This is a line in the xml files where I need to replace sbteev510b2bsbt. “sbteev510b2bsbt” will be different every time.
What if you don’t know what you are replacing but know where it is: I know the line begins with Content-ID and I want to replace sbteev510b2bsbt
I have been scouring and reading but not finding the answer. Thank you!
That’s the moment where the sophisticated regex pattern appears on the stage …
$date = Get-Date -format “yyyyMMdd”
‘Content-ID: sbteev510b2bsbt-rn20-preamble’ -replace '(?<=Content-ID:\s+).+?(?=-rn20-preamble)',$date
You just need to know the “structure” of the ID or the possible format. If it’s always something between a white space and a “minus” … it will fit.
Wow; you made that SO EASY!!! Thank you for teaching!