Solved

Using Powershell to Search & Replace

Posted on 2009-05-07
2
824 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:AWOOD-HILL
2 Comments
 
LVL 70

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:AWOOD-HILL
ID: 31579229
That did the trick!  Thank you very much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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)

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now