p6srwin.pl
Search and replace text in one or more files, including all files in a tree.
Note: This script was written, tested, and intended to be used on Windows machines with a Windows version of Perl. It was tested against Perl version 5.6.1, which is freely available at http://www.activestate.com/Products/Download/Register.plex?id=ActivePerl.
p6srwin.PL srchstr replstr [-lLogFile] FileSpec or wildcards [/s] [/b]
[/s] = Search subdirectories.
[/b] = Make backups of changed files.
OR: -c [-lLogFile] FileSpec or wildcards [/s] for console input
OR: -f[CtlFile] to take all parameters from CtlFile
If LogFile is not specified, default is P6SRWIN.LOG in the current working directory.
The easiest way to handle complex or repetitive operations is to create a configuration file.
SrchStr=[Regex] ReplStr=[ReplaceString] ReplStrIsExpr=[Y|N} FQFN=[FileSpec] RecurseDirs=[Y|N} CmdFileFQFN=[FileSpec] TargetDir=[DirNameWithBackSlash]
[ReplaceString] = Any valid replacement string unless ReplStrIsExpr = Y. In that case,
it may be any valid Perl expression that returns a string, which becomes the replacement string.
ReplStrIsExpr = Optional flag that, if set to Y, then [ReplaceString] must be a valid
Perl expression. See Description below for further details.
[FileSpec] = For FQFN, this can be any valid file specification. Wild cards are permitted.
For CmdFileFQFN, this must, of course, be a valid file name, which should have an extension
appropriate to your platform (.BAT for Windows 9x; .CMD or .BAT for Windows NT/2000/XP/2003).
[DirNameWithBackSlash] is the target directory for the copy commands that will be written into
CmdFileFQFN. A trailing backslash will be appended if necessary.
SrchStr=P6C_c_3dx\.gif.*?> ReplStr=P6_Generic_111.gif" width="111" height="106" alt="WizardWrx Logo"> ReplStrIsExpr=N FQFN=P:\*.html RecurseDirs=y CmdFileFQFN=J:\P6PUBL\WEBSITES\P6\Mass_Updates\P6SRWIN_Replace_Logo_Links_20030811_5_COPY.CMD TargetDir=J:\TEMP\ SafetyNet=Y
File to be searched. If unqualified, assumed to be in CWD.
/B option is specified by command line or the
SafetyNet value is included in the ocnfiguration file and set to Y.
CmdFileFQFN and TargetDir
values are both specified in the configuration file.
This script processes text files, finding and replacing arbitrary strings.
Be careful with this script - it accepts wildcards and processes every text file that meets the wildcard criteria. This could be a catastrophe in the hands of the unwary.
If you are unsure of what you are doing, I strongly urge you to take advantage of the new /B command
line option or its corollary, the SafetyNet configuration file option.
Backup files, if specified, are created by adding the extension .BACKUP to the original file name.
The resulting file has a double extension, so the original extension is preserved. I did this to prevent
name collisions.
The option of generating a command script to copy changed files is available only through the
configuration file. If you generate a shell script, the last command in the script calls P6PAUSE.COM,
which is included in this distribution.
P6PAUSE.COM is a general purpose MS-DOS batch file pause program that I wrote in 1990 because I got
tired of the lame PAUSE command that is built into the shell treating just any old key on the
keyboard as an instruction to resume the batch file. Unlike the built-in command, P6PAUSE responds to
only three things:
CTRL-C
CTRL-BREAK
If ReplStrIsExpr is specified and set to Y, then [ReplaceString] must be a valid Perl expression
that evaluates to the desired replacement string. Since this evaluation happens for every replacement,
the expression can use the global regular expression variables to take into account the value of the match
string in creating its replacement. This very powerful feature requires a knowledge of Perl, including
variable scope and the limitations imposed by the Strict module. These matters are beyond the scope of
this document. However, to give you an idea of its power, I offer this example from a configuration file
that we used two years ago to globally replace a JavaScript function call in a Web site.
Here are the search and replace strings from that configuration file.
SrchStr=javascript:open_popup\(\'(.*)\'\)
ReplStr=$replacewith = "javascript:p6new_br('$1','','t','640','450')"
The subexpression (.*) matches whatever argument (a URL) was passed to the open_popup function in
the original Web page. The $1 global regular expression variable substitutes the argument into the
replacement function call.
This script incorporates inline two functions from our unpublished library of Perl modules. We put them inline so that we can distribute this script without the burden of the unpublished modules, for which no standard installers exist yet.
Date Who What ========== === ========================================================================================= 06/10/2000 DAG Initial release.
02/27/2001 DAG 1) Add code to process wild cards and subdirectories by using internal system commands
that pipe their output to this program. Remove ability to process a list of files.
2) Add code to identify, log, and skip directories. Yea!
3) Modify the formatting of the date and time per the note that I found and sent to
David Camm about correct formatting of dates using the printf function, with a few
modifications to handle the spelled month and weekday. Yea!
03/02/2001 DAG 1) Add ability to pass in the whole lot of parameters in a file.
2) Fix typo in die statement for failed open of log file.
3) Simplify to use a unary filetest operator to identify directories when a file open
fails.
03/05/2001 DAG 1) Add M switch to search and replace statement in Process subroutine.
2) Add two search and replace statements each to map the \n and \r sequences in the
search and replace strings to the corresponding control characters.
03/07/2001 DAG Add code to record the date and time when processing was finished.
03/15/2001 DAG Fix discrepencies in error messages identified while transferring code for skipping
directories and other files that cannot be opened to p6grep.pl.
05/01/2001 DAG Add the Tab character to the list of substitutions along with Newline and Return.
05/27/2001 DAG 1) Add support for comments in control files. Comments are identified as lines that
begin with a "#" character, just as in Perl.
2) Add seconds to the time stamp displays.
3) Force the time stamp to update at end of job.
4) Simplified control file parser to take full advantage of split control records.
5) Add capability to pass in an expression for evaluation by the EVAl function
so that I can incorporate backreferences in the replacement string.
6) Fix bug that prevented embedded "=" characters in the search and replace strings.
08/16/2003 DAG 1) Add optional export of a shell script to copy the changed files elsewhere.
2) Add Alarm (bell) characters to all the abort and warning messages.
3) Add code to shell script generator to handle file names with embedded spaces.
4) Add option to make backups of changed files.
08/25/2003 DAG Correct typographical error in documentation of disp_time_med_p6c function.
No code was changed.
03/15/2004 DAG 1) Correct display of replacements count for case where no files are changed.
2) Suppress message when no command file is generated.
3) Correct display of file names that contain embedded spaces.
4) Improve effficiency of configuration file parser by having it bypass remaining
tests after it has successfully parsed a line.
03/17/2004 DAG Correct error in processing of expressions used as replacement strings.
02/07/2005 DAG Correct formatting error in a file open ABORT message and remove an extra print
statement.
This script was created by adapting sr, written 05/30/1996 by Paul Lutus, lutusp@arachnoid.com of
Ashland, Oregon, and began life as an exercise in learning Perl.
Following is complete documentation for two functions that I imported from unpublished Perl modules so that I can distribute this script separately and so that it can be used with the standard Perl distribution.
Given a pair of file names, the first of which must exist and the second of which may exist,
depending on the value of the $pflags argument, rename or copy the source file as dictated by the
flags.
$string = rename_file_p6c ( $sourcefn , $destfn , [ $pflags ] , [ $plogfh ] ) ;
The first argument, $sourcefn, is a string which must contain a file name that is either fully
qualified or located in the current working directory. This file must exist.
The second argument, $destfn, is a string which must contain a file name that is either fully
qualified or located in the current working directory. This file may exist if the flags allow it.
The third, optional, argument, $pflags, governs several aspects of processing as shown in the
table below.
+------+------+--------------------------------------------+--------------------------------------------+ | Bit | TRUE | Meaning if ON (TRUE) | Meaning if OFF (FALSE) | +------+------+-----------------------------------------------------------------------------------------+ | 0 | 1 | UNUSED and reserved for compatibility with companion functions in this module | +------+------+-----------------------------------------------------------------------------------------+ | 1 | 2 | Flag errors as FATAL | Flag errors as WARNINGS | +------+------+--------------------------------------------+--------------------------------------------+ | 2 | 4 | OK to overwrite existing file | Overwrite of existing file forbidden | +------+------+-----------------------------------------------------------------------------------------+ | 3 | 8 | UNUSED and reserved for compatibility with companion functions in this module | +------+------+-----------------------------------------------------------------------------------------+ | 4 | 16 | UNUSED and reserved for compatibility with companion functions in this module | +------+------+-----------------------------------------------------------------------------------------+ | 5 | 32 | UNUSED and reserved for compatibility with companion functions in this module | +------+------+-----------------------------------------------------------------------------------------+ | 6-31 | | UNDEFINED [reserved for future use] | UNDEFINED [reserved for future use] | +------+------+--------------------------------------------+--------------------------------------------+
The fourth argument, $plogfh, is a optional typeglob that points to a filehandle for a log file.
Use it, if one is supplied, to record error messages in a permanent form.
The return value from rename_file_p6c is True if the operation succeeded, else it is False.
Call get_last_error_p6filelib and/or get_last_errormsg_p6filelib to get detailed information about
the precise cause of the failure.
The rename_file_p6c function makes a backup or renames a file, depending on settings in the bit mask
of flags that is passed as an optional third argument.
The rename_file_p6c prototype is ($$;$$).
This function was adapted from a like named function in my P6consulting::P6filelib perl module, which
is available separately and contains several other useful file processing functions.
Display time for reports in Medium format.
$string = disp_time_med_p6c ( [ $pprefix ] , [ $psuffix ] , [ $pwhat ] , [ $ptz ] ) ; print FILEHANDLE $string ;
The first argument, $pprefix, is the prefix to include in the string before the time. You may pass an
empty string if all you want returned is the time string.
The second argument, $psuffix, is the suffix to append to the string after the time. You may pass an
empty string if all you want returned is the time string.
The third argument, $pwhat, specifies whether to return the date and time, only the date, or only the
time. Permissible values are:
1 = Date and Time 2 = Date only 3 = Time only
If the argument is null or undefined, date and time are both returned.
The fourth argument, $ptz, is the time zone to use. Set it to True (1) for GMT.
Otherwise, the returned time string will be local time.
Note: At present, the time zone is hard coded as US Central.
None of the arguments is mandatory.
The disp_time_med_p6c prototype is (;$$$$).
The return value from disp_time_med_p6c is a time and date string suitable for display on a report or
writing into a log file.
The disp_time_med_p6c function can be used to display either local time or GMT.
This function was incorporated as is from Perl module P6consulting::P6timelib, available
separately. That module contains several other generally useful time functions.
Used File::Listing from the standard distribution as a template, as this function, and its companion,
disp_time_long_p6c were the first two functions that I wrote that I intended to keep in a Perl module.
Archive p6srwin.zip contains p6srwin.pl and a copy of this documentation page.
The MD5 digest of this file is 1687dd364e7847546de956cd24805d24. You
can use MD5WIN to verify the MD5 digest of this
and other files.
David A. Gray, MBA, North Richland Hills, Texas, USA http://www.wizardwrx.com Use this form to contact the author.
©2000-2005, David A. Gray, of Simple Soft Services, Inc., /d/b/a WizardWrx. All rights reserved world wide.
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.