Friday, October 3, 2014

Powershell - Find/Replace syntax all files in a directory

A co-worker asked me if a script I had previously written would would work for what he was trying to do.  After talking to him about what he was trying to accomplish, I wrote this little script.

He's a Cisco Engineer and needed to replace multiple iterations of server names and IP addresses spread over  hundred of config files.  For his purposes, he asked if it could read each file, find the address that needed to be changed and write that line to a new file with the replaced value.

This script will do that, by default, I've set it to recreate the file with all lines, including the changed values.  A simple value change will flip it to only write the lines that changed.

So, without further ado, this script will search all files in a directory, do as many find/replaces as you tell it to and create all the files in a new directory.


# ===============================================================================
# Author:  Mike Sweany, CDW
# Date:    9/29/2014
# Version: 1.1
# Purpose: This script will search all txt files in a directory line by line and do a mass find/replace
# By default, it will create a new file with all the unchanged lines plus the find/replaces
# if you'd like to see only the changed lines, change $createAll to 0
# ===============================================================================
# Make sure this script is in the same folder that has the config folder in it.  Also you need
# to create the destination folder (where changes will go) in this same directory


# The source folder that contains all of the original configs (must already exist) 
$sourceFolder = "Source Files"
# The folder where config changes will be saved (must already exist) 
$destinationFolder ="Changed Files"
# The exact string(s) to find. Add extra comma delimited strings in "quotes"
$find = @("10.10.10.10","10.10.10.20","oldserver.something.local") 
# The string(s) that will replace what is found (make sure there are the same amount of values as in $find)   
$replace = @("10.10.0.10","10.10.0.20","newserver.something.local")
## For both find and replace if you only have one value it looks like this
$example = @("my only value")
## set to 0 if you only want to see the changed lines
$createAll = 1


######################################################################################################
##################################### Do Not Edit Below this line ####################################
$destinationfolderpath = get-location

Get-ChildItem $sourceFolder | foreach {
    # set the file name we're working on 
    $file = $_.Name
    Write-Host "Checking file" $file
    $content = Get-Content $sourceFolder\$_ | % {
        for ($i=0; $i -lt $find.length; $i++) {
            if ($_ -match $find[$i]) {
                $new = $_ -replace $find[$i], $replace[$i] 
                Add-Content $destinationFolder\$file "$new"
                Write-Host "Found" $find[$i] "in" $file ", logged changes in" $destinationFolder\$file -ForegroundColor green
            }else{
                if($createAll -eq 1){
                    Add-Content $destinationFolder\$file "$_"
                }
            }
        }
    }
}

To run this, save the file as massFindReplace.ps1 in the same root folder as "Source Files" and "Changed Files", edit the file to set your variables (Folder names, find and replace strings etc..).  Save it , then run the script.  As the script is running, it will write to the console what file it's on and as it does a find/replace.

You can download the file here also

No comments:

Post a Comment