[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using Powershell to Search & Replace

Posted on 2009-05-07
2
Medium Priority
?
832 Views
Last Modified: 2012-05-06
I'm helping the web guy replace some lines in files that are on our web server and I thought I found a quick way to complete it.  I borrowed the below code from somewhere on the internet, and it works well for finding and replacing simple text, but it won't find the string I've filled in (behind the $old and $new.)  I don't use Powershell often, so I'm sure it's just some special characters that need to be escaped out, but I'm not sure.  Any help would be greatly appreciated.

function ReplaceText($fileInfo)
{
    if( $_.GetType().Name -ne 'FileInfo')
    {
        # i.e. reject DirectoryInfo and other types
         return
    }
    $old = 'require($_SERVER["DOCUMENT_ROOT"] . "/html_temp_sets/pageset.php");'
    $new = 'require("../../includes/php/pageset.php");'
    (Get-Content $fileInfo.FullName) | % {$_ -replace $old, $new} | Set-Content -path $fileInfo.FullName
    "Processed: " + $fileInfo.FullName
}

$loc = 'c:\my file\location'
cd $loc
$files = Get-ChildItem . -recurse

$files | % { ReplaceText( $_ ) }
0
Comment
Question by:Allen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 200 total points
ID: 24334639

-Replace uses a Regular Expression for the search value. That can give you some really odd results if you include special characters in your $Old value.

Rather than worrying too much about it you can call the Replace Method on the string (rather than using -Replace). You lose the power of the Regular Expression search, but you don't have to worry about special characters in the strings.

e.g.

...
{ $_.Replace($Old, $New) }
...

Chris
0
 

Author Closing Comment

by:Allen
ID: 31579229
That did the trick!  Thank you very much!
0

Featured Post

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question