The following code doesn't error, but also doesn't update the matching string in the XML files. I'm treating it just like a text file since code to parse it as an XML file failed. I'd like to just get this working as it can be used for more than just XML files.
$files = Get-ChildItem -Recurse -Filter "*.xml"
# If the PowerShell string is quoted with single quotes, then the single quote characters cannot be escaped with the backtick "`"
# You must double the embedded single quotes (replace any embedded ' characters with '')
# Double quote characters would not need to be escaped in a single quoted string
$find = '<?xml-stylesheet type=''text/xsl'' href='' alternate=''no'' title=''Allscripts Default''?>'
$replace = '<?xml-stylesheet type=''text/xsl'' href=''CCD.XSL'' alternate=''no'' title=''Allscripts Default''?>'
foreach ($file in $files)
{
$content = Get-Content $file.FullName
if(Select-String -InputObject $content -Pattern $find -Quiet) {
$content |
Foreach-Object { $_ -replace $find, $replace } |
Set-Content $file.Fullname
}
}
Comparing my pattern string to one of the XML files (looking for a typo):
$find = '<?xml-stylesheet type=''text/xsl'' href='' alternate=''no'' title=''Allscripts Default''?>'
Excerpt from top of one of the XML files in the folder:
<?xml-stylesheet type='text/xsl' href=' alternate='no' title='Allscripts Default'?><ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi=" code="US" /><typeId extension="POCD_HD000040" root="2.16.840.1.113883.1.3" />
I figured out how to debug the code in PowerShell ISE (Still a newb)
* The XML files in the folder of the script are picked up as expected in $Files
* Using two single quotes seems to work great to escape the individual single quotes in the find/replace strings, which is a relief
* F11 through the script, which flows to the foreach statement, moves to the first $content line, moves to the if(Select-String ... line where I can hover over $content and can't read it all, but it shows the content of the XML file past just the $find section - No spaces between the XML elements as we have it (FYI - This is not a well-formed XML file in other aspects hence treating it like an ordinary file) and it jumps to the next file in the loop like it never matches the $find string.
Is my Select-String syntax off? Thanks for any guidance !!
$files = Get-ChildItem -Recurse -Filter "*.xml"
# If the PowerShell string is quoted with single quotes, then the single quote characters cannot be escaped with the backtick "`"
# You must double the embedded single quotes (replace any embedded ' characters with '')
# Double quote characters would not need to be escaped in a single quoted string
$find = '<?xml-stylesheet type=''text/xsl'' href='' alternate=''no'' title=''Allscripts Default''?>'
$replace = '<?xml-stylesheet type=''text/xsl'' href=''CCD.XSL'' alternate=''no'' title=''Allscripts Default''?>'
foreach ($file in $files)
{
$content = Get-Content $file.FullName
if(Select-String -InputObject $content -Pattern $find -Quiet) {
$content |
Foreach-Object { $_ -replace $find, $replace } |
Set-Content $file.Fullname
}
}
Comparing my pattern string to one of the XML files (looking for a typo):
$find = '<?xml-stylesheet type=''text/xsl'' href='' alternate=''no'' title=''Allscripts Default''?>'
Excerpt from top of one of the XML files in the folder:
<?xml-stylesheet type='text/xsl' href=' alternate='no' title='Allscripts Default'?><ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi=" code="US" /><typeId extension="POCD_HD000040" root="2.16.840.1.113883.1.3" />
I figured out how to debug the code in PowerShell ISE (Still a newb)
* The XML files in the folder of the script are picked up as expected in $Files
* Using two single quotes seems to work great to escape the individual single quotes in the find/replace strings, which is a relief
* F11 through the script, which flows to the foreach statement, moves to the first $content line, moves to the if(Select-String ... line where I can hover over $content and can't read it all, but it shows the content of the XML file past just the $find section - No spaces between the XML elements as we have it (FYI - This is not a well-formed XML file in other aspects hence treating it like an ordinary file) and it jumps to the next file in the loop like it never matches the $find string.
Is my Select-String syntax off? Thanks for any guidance !!