Solved

Using Powershell to Search & Replace

Posted on 2009-05-07
2
828 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 50 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

730 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