Searching and replacing text strings across multiple files

Clockworks

Supreme [H]ardness
Joined
Aug 23, 2004
Messages
5,269
We need the ability to search and replace for a text string across multiple files (one entire directory at a time would be ideal) on a server or a local machine.

Example: replace all occurrences of '\\Contoso\jobs\' with '\\Contoso\data\jobs' within a group of .vbs script files

I found a piece of software called Actual Search & Replace. It seems to work well on a local machine for general text but I was wondering if there were something else that I should research that could do shared folders on a server and .vbs script files.
 
If you don't mind mixing in cygwin, or a linux box, read up on `sed`.

I would also change the file to include the location from an external file, while you are at it. It wouldn't be too much more work than the change you are wanting to implement, and would allow you to make changes like this more easily in the future.
 
Actually, Search and Replace is pretty damn good. Only downside is it's inability to search SQL files, so we might need something for that. =/
 
Perl should be able to do that.
$previous = "\\Contoso\jobs\"
$current = "\\Contoso\data\jobs"
s/$previous/$current/g

I forget how to open files and read through them, but if you put the above statements in it should go through any file and replace the path for you.
 
love unix
be sure to copy to a temp folder and apply the changes to the temp folder
also some special characters may need to be escaped
the program probably will not handle them correctly

Code:
############
#
# greplace - global replace
#
############

usage() {
  echo \
"
greplace BEFORE AFTER <filenames>
     changes the first instance of BEFORE to AFTER in each line of <filenames>
     and reports on the differences.
Examples:
     greplace TX Texas */addresses.*
     greplace ii counter2 *.c*

"
     exit 0
}

case $1 in
     -h | --help) usage;;
esac

if test $# -lt 3
then
     usage
fi

TMPDIR=/tmp
TMPFILE=$TMPDIR/chstr.$$

BEFORE=$1
AFTER=$2

shift;shift

for FILE in $*
do
    sed -e "s/$BEFORE/$AFTER/" $FILE >$TMPFILE
    if [ $? -eq 0 ]; then
    echo "$FILE"
    diff $FILE $TMPFILE
    echo ""
    mv $TMPFILE $FILE
    fi
done
 
Beyond Compare can also handle this, in addition to offering many other potentially useful features.
 
Depending on why you're doing this, you might want to consider rewriting this mess of hard-coded paths in such a way that, if you want to do it again, you only have to make a single change in one spot & everything else just works. You say they're all VBS files - why not have them all include a common config file & build paths based on a single ROOT_PATH variable?
 
This is easily done on Windows too, with powershell, which is installed by default in Windows 7:
<greplace.ps1>
Code:
if (($args.count -eq 1 -and $args[0] -eq "-h") -or $args.count -ne 3)
{
	""
	"greplace BEFORE AFTER <filenames>"
	"	Replace BEFORE with AFTER in <filenames>"
	"Examples:"
	"	greplace.ps1 line test audioconfig.txt"
	"	greplace.ps1 echo write *.ps1"
	exit
}

foreach ($file in (dir $args[2]))
{
	$content=(cat $file)
	$content=($content -replace $args[0], $args[1])
	set-content $file $content
}

I tried to mirror the functionality of "jiminator's" unix script, but I didn't include anything like "diff" because I'm not familiar with it, I'm sure it's easy to duplicate if necessary. http://blogs.technet.com/b/heyscrip...ell-to-replace-characters-in-a-text-file.aspx has a list of characters that need to be escaped, either with a "\" or a: ` - something to keep in mind.
 
Last edited:
Back
Top