Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Find/Replace String Fails with No Error

Find/Replace String Fails with No Error

Find/Replace String Fails with No Error

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=''https://ced-a.ms.allscriptscloud.com/ced/3.2/ced.x...'' 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=''https://ced-a.ms.allscriptscloud.com/ced/3.2/ced.x...'' alternate=''no'' title=''Allscripts Default''?>'

Excerpt from top of one of the XML files in the folder:
<?xml-stylesheet type='text/xsl' href='https://ced-a.ms.allscriptscloud.com/ced/3.2/ced.x...' alternate='no' title='Allscripts Default'?><ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><realmCode 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 !!

RE: Find/Replace String Fails with No Error

As noted in my contents, those are 2 single quotes in the $find and $replace strings, not double quotes. After I posted I saw how it looked so adding this comment ... Same thing goes for further down in the post

RE: Find/Replace String Fails with No Error

If any clarifications are needed, please let me know.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close