Solved

Using Powershell to Search & Replace

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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 the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

717 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