1

I currently have several files that I'm using powershell with the hopes of creating one output file.

I have a template file with key words in it. I have several files with contents that belong in the middle of the template. And I have my list for what I need to replace.

text file Template:

<pi.category>
<figure id="fig6"><title>TITLEHERE</title>
<graphic boardno="GRAPHBOARDNO"></graphic>
</figure>
<fncgrp>
<fnccode>FGCCODE</fnccode>
<fnctitle>FGCTITLE</fnctitle>
</fncgrp>ADDFILETEXTHERE</pi.category>

Second text file, to be inserted at "ADDFILETEXTHERE" (potentially, it could be anything here):

<pi.item><callout assocfig="fig1" numref="c1"/><qty>8</qty>
<smr maintcode="FZ" recovercode="Z" sourcecode="PA"/><nsn><fsc>5310</fsc>
<niin>00-763-8921</niin></nsn><partno>MS51967-23</partno><cageno>96906</cageno>
<name>NUT,PLAIN,HEXAGON</name><desc></desc></pi.item>

And my powershell script:

$content = [IO.File]::ReadAllText(".\insert.txt")
Get-Content C:\template.xml |
Foreach-Object {$_ -replace "R00000", "R00001"} |
Foreach-Object {$_ -replace "TITLEHERE", "Standard Practices"} |
Foreach-Object {$_ -replace "FGCCODE", "01520"} |
Foreach-Object {$_ -replace "FGCTITLE", "Hydraulic Line Stuff"} |
Foreach-Object {$_ -replace "GRAPHBOARDNO", "AVLBCP0123"} |
Foreach-Object {$_ -replace "ADDFILETEXTHERE", "$content"} |
Set-Content C:\output.xml

I can get all of the replace stuff to work, but I can't get the contents of the insert txt file to replace the "addfiletexthere".

How can I replace a keyword with the contents of an existing text file??

1 Answers1

0

This is your problem (notice my current working directory in PowerShell and the path in the error):

PS C:\Users\Frode\Desktop> $content = [IO.File]::ReadAllText(".\insert.txt")
Exception calling "ReadAllText" with "1" argument(s): "Could not find file 'C:\WINDOWS\system32\insert.txt'."
At line:1 char:1
+ $content = [IO.File]::ReadAllText(".\insert.txt")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

.NET Methods lives in it's own world when it comes to current directory. See my earlier answer about this: Create new absolute path from absolute path + relative or absolute path

You could see a solution in the question I linked to above, or you could simply use Get-Item to get a FileInfo-Object for the file and use it's FullName-property to get the full path. Ex:

$insertFile = Get-Item ".\insert.txt"
$content = [IO.File]::ReadAllText($insertFile.FullName)

#Test
@"
<pi.category>
<figure id="fig6"><title>TITLEHERE</title>
<graphic boardno="GRAPHBOARDNO"></graphic>
</figure>
<fncgrp>
<fnccode>FGCCODE</fnccode>
<fnctitle>FGCTITLE</fnctitle>
</fncgrp>ADDFILETEXTHERE</pi.category>
"@ -split [System.Environment]::NewLine | 
Foreach-Object {$_ -replace "R00000", "R00001"} |
Foreach-Object {$_ -replace "TITLEHERE", "Standard Practices"} |
Foreach-Object {$_ -replace "FGCCODE", "01520"} |
Foreach-Object {$_ -replace "FGCTITLE", "Hydraulic Line Stuff"} |
Foreach-Object {$_ -replace "GRAPHBOARDNO", "AVLBCP0123"} |
Foreach-Object {$_ -replace "ADDFILETEXTHERE", "$content"}

#Output
<pi.category>
<figure id="fig6"><title>Standard Practices</title>
<graphic boardno="AVLBCP0123"></graphic>
</figure>
<fncgrp>
<fnccode>01520</fnccode>
<fnctitle>Hydraulic Line Stuff</fnctitle>
</fncgrp><pi.item><callout assocfig="fig1" numref="c1"/><qty>8</qty>
<smr maintcode="FZ" recovercode="Z" sourcecode="PA"/><nsn><fsc>5310</fsc>
<niin>00-763-8921</niin></nsn><partno>MS51967-23</partno><cageno>96906</cageno>
<name>NUT,PLAIN,HEXAGON</name><desc></desc></pi.item></pi.category>
Community
  • 1
  • 1
Frode F.
  • 52,376
  • 9
  • 98
  • 114