The Perl script batchReplace. pl can be used to replace text/code in files in batches. You can search for files of the specified type in the specified directory and recursively check the subdirectories. Copy the directory structure of the input file when outputting the file.
The ]win32application batchreplace.exe is an executable program compiled by a Perl script. It can be executed without installing the Perl runtime environment.
Use
BatchReplace.exe [-I input file path (or directory containing files)] [-o output file location (file or directory)] [-c batch input file extension, with ". ". multiple extensions are separated by" | ".] [-m matching mode] [-I (case-insensitive Matching content)] [-G (global search for the content to be matched)] [-e is an exception string that supplements the matching mode. If such a string is found in the matching result, do not match] [-r replaced content]
The preceding parameters have no order restrictions. When the file path involved by the-o parameter does not exist, it is automatically created. When the output file already exists, the original content of the file will be overwritten. (For security reasons, do not output data to the original location of the input file to avoid irreparable loss .)
For example:
BatchReplace.exe-I d: \ gaoshu1-o d: \ do \ 123 \ 456-e http://www.blueidea.com/
Set parameters through the configuration file batchReplace. set
The configuration file may contain the following settings (format example, case sensitive ):
Input = E: \ fna \ specifies the Input path, which is equivalent to the default value of the command line parameter-I.
-I = E: \ fna \ same as above.
Output = E: \ DNA walks \ specifies the Output path, which is equivalent to the default value of the command line parameter-o.
-O = E: \ DNA walks \ same as above.
Match = <iframe [^>] *> [\ s \ S] *? <\/Iframe> the matching mode, which is equivalent to the default value of the command line parameter-m.
-M = <iframe [^>] *> [\ s \ S] *? <\/Iframe>.
Insensitive ignores the uppercase and lowercase letters of the matching content, which is equivalent to the command line parameter-I.
-I is the same as above.
Global searches for the content to be matched, which is equivalent to the command line parameter-G.
-G is the same as above.
Replacement = -R = Except T = http://www.blueidea.com/exception strings, such as strings found in the matching results, do not match, equivalent to the default value of the command line parameter-e.
-E = http://www.blueidea.com/same as above.
Checktype).htm |. html when the input parameter is set to a directory, process the text files containing these extensions in the directory (recursive check subdirectories ). It is equivalent to the default value of-c.
-C is the same as above.
Place a project in each row. Do not include any spaces or quotation marks between the content.
All configuration items are optional except for necessary input and output. The command line parameters overwrite the corresponding default values. If the configuration file does not contain Match or CheckType, the built-in default value is enabled (as shown in the preceding example ). Replacement is a null character by default. Replace the Matching content with a null character, that is, clear the Matching content.
To retain the historical configuration in batchReplace. set, you can place any non-blank characters before the parameter to cancel the function.
For example:
2007/04/06 Insensitive
(Insensitive will be retained as the historical configuration and will not take effect .)
2007/04/06 Replacement = (Replacement =
If the same project appears in multiple lines of the configuration file, the final settings will prevail. For example:
Checktype).htm |. html
CheckType =. jsp
-C =. asp |. php
Set the CheckType value to. asp |. php. You can also use this feature to keep the historical configuration for debugging convenience.
This script was written in a rush (it was written during dinner) And I will improve it later. (For this reason, please indicate the source and pay attention to the update .)
[Disclaimer] the author shall not bear any unexpected losses arising from the use of this tool.
Perl source codeCopy codeThe Code is as follows: # file: batchReplace. pl
# Author: Bound0
# Created: 2007-04-06
# First published: http://bbs.blueidea.com/viewthread.php? Tid = 2734388
My $ match;
My $ replacement = '';
My $ insensitive = 0;
My $ global = 0;
My $ gi;
My $ go;
My $ Checktype = ". htm |. html ";
My $ response T;
If (open (setfile, "<batchReplace. set "))
{
While (<setfile>)
{
If (/^ \ s *-I/) {$ insensitive = 1}
If (/^ \ s *-G/) {$ global = 1}
If (/^ \ s *-m = (. +)/) {$ match = $1}
If (/^ \ s *-r = (. +)/) {$ replacement = $1}
If (/^ \ s *-e = (. +)/) {$ hour T = $1}
If (/^ \ s *-I = (. +)/) {$ gi = $1}
If (/^ \ s *-o = (. +)/) {$ go = $1}
If (/^ \ s *-c = (. +)/) {$ Checktype = $1}
If (/^ \ s * Insensitive/) {$ insensitive = 1}
If (/^ \ s * Global/) {$ global = 1}
If (/^ \ s * Match = (. +)/) {$ match = $1}
If (/^ \ s * Replacement = (. +)/) {$ replacement = $1}
If (/^ \ s * random t = (. +)/) {$ random T = $1}
If (/^ \ s * Input = (. +)/) {$ gi = $1}
If (/^ \ s * Output = (. +)/) {$ go = $1}
If (/^ \ s * CheckType = (. +)/) {$ Checktype = $1}
}
}
My $ para = ''. join ('', @ ARGV );
If ($ para = ~ /-I */) {$ insensitive = 1}
If ($ para = ~ /-G */) {$ global = 1}
My @ ti = split (/-I */, $ para );
If ($ ti [1]) {($ gi) = split (/-(o | I | c | e | m | r | I | G )/, $ ti [1])}
Unless ($ gi) {print "No \" Input path \ "parameter! "; Exit}
My @ to = split (/-o */, $ para );
If ($ to [1]) {($ go) = split (/-(o | I | c | e | m | r | I | G )/, $ to [1])}
Unless ($ go) {print "No \" Output path \ "parameter! "; Exit}
My @ tc = split (/-c */, $ para );
If ($ tc [1]) {($ Checktype) = split (/-(o | I | c | e | m | r | I | G )/, $ tc [1])}
My @ te = split (/-e */, $ para );
If ($ te [1]) {($ te T) = split (/-(o | I | c | e | m | r | I | G )/, $ te [1])}
My @ tr = split (/-r */, $ para );
If ($ tr [1]) {($ replacement) = split (/-(o | I | c | e | m | r | I | G )/, $ tr [1])}
Unless ($ match) {$ match = "<iframe [^>] *> [\ s \ S] *? <\\/ Iframe> ";
$ Insensitive = 1;
$ Global = 1}
My @ tm = split (/-m */, $ para );
If ($ tm [1]) {($ match) = split (/-(o | I | c | e | m | r | I | G )/, $ tm [1])}
Unless ($ match) {print "No \" Match Pattern \ "parameter! "; Exit}
My $ checktyp = '(';
$ Checktype = ~ S/\./\./g;
$ Checktype = ~ S/\ |/\) \ | \ (/g;
$ Checktyp. = $ Checktype. ') $ ';
My $ excep;
If ($ snapshot t ){
$ Excep = $ response T;
$ Excep = ~ S/\/g;
$ Excep = ~ S/\./\./g;
$ Excep = ~ S/\ |/g;
$ Excep = ~ S/\ [/\ [/g;
$ Excep = ~ S/\]/g;
$ Excep = ~ S/\ (/g;
$ Excep = ~ S/\)/g;
$ Excep = ~ S/\ $/g;
$ Excep = ~ S /\? /\\\? /G;
}
My $ replacemen;
If ($ replacement ){
$ Replacemen = $ replacement;
$ Replacemen = ~ S/\/g;
$ Replacemen = ~ S/\./\./g;
$ Replacemen = ~ S/\ |/g;
$ Replacemen = ~ S/\ [/\ [/g;
$ Replacemen = ~ S/\]/g;
$ Replacemen = ~ S/\ (/g;
$ Replacemen = ~ S/\)/g;
$ Replacemen = ~ S/\ $/g;
$ Replacemen = ~ S /\? /\\\? /G;
}
Sub cFile
{
My $ fi;
($ Fi) = @_;
If (opendir (DIR, $ fi ))
{
My @ dir = readdir (DIR );
Closedir DIR;
If ("\" eq substr $ fi, (length $ fi)-1) {$ fi = substr ($ fi, 0, (length $ fi)-1 )}
My @ subdirs = grep {/^ (?! \.)/&-D "$ fi \\$ _"} @ dir;
Foreach my $ subdir (@ subdirs)
{
CFile ("$ fi \ $ subdir ")
}
@ Files = grep {/$ checktyp/I &-T "$ fi \ $ _"} @ dir;
Foreach my $ fil (@ files)
{
My $ bp = '';
$ Bp = (substr $ fi, (length $ gi ))."\\";
My $ bi = "$ fi \ $ fil ";
My $ bo = $ go. $ bp. $ fil;
Remove ($ bi, $ bo)
}
}
}
Unless ("\" eq substr $ go, (length $ go)-1) {$ go. = "\\"}
If (-d $ gi)
{
Unless ("\" eq substr $ gi, (length $ gi)-1) {$ gi. = "\\"}
CFile ($ gi );
}
Else
{
My $ bu = substr $ gi, (rindex $ gi ,'\\');
My $ bo = $ go. $ bu;
Remove ($ gi, $ bo)
}
Print "\ nProcess Finished! ";
Print "\ n-I: $ gi ";
Print "\ n-o: $ go ";
Print "\ n-m: $ match ";
If ($ response t) {print "\ n-e: $ response t "}
If ($ replacement) {print "\ n-r: $ replacement "}
Sub remove
{
My $ bi;
My $ bo;
($ Bi, $ bo) = @_;
Print "\ nprocessing $ bi... \ n ";
Unless (open (INPUT, "<$ bi") {print "\ n [Warn] Can not open the file <$ bi >:$! "; Return}
My @ conts = <INPUT>;
Close INPUT;
My $ cont = join '', @ conts;
My $ c;
If ($ insensitive)
{
If ($ global)
{
Unless ($ cont = ~ S/($ match)/$ {$ c = c0000t ($1)} $ c/gi) {die "$! "}
}
Else
{
Unless ($ cont = ~ S/($ match)/$ {$ c = c0000t ($1)} $ c/I) {die "$! "}
}
}
Else
{
If ($ global)
{
Unless ($ cont = ~ S/($ match)/$ {$ c = c0000t ($1)} $ c/g) {die "$! "}
}
Else
{
Unless ($ cont = ~ S/($ match)/$ {$ c = c0000t ($1)} $ c/) {die "$! "}
}
}
Unless (open (OUT, "> $ bo "))
{
If ($! = 2)
{
My $ dbo = substr $ bo, 0, (rindex $ bo ,'\\');
If (opendir (OUTDIR, $ dbo) {closedir OUTDIR; print "\ n [Warn] Can not open the output file <$ bo >:$! "; Exit}
Else
{
If ($! = 2)
{
Unless (pmkpath ($ dbo) {print "\ n [Warn] Can not creat the output directory <$ dbo >:$! "; Exit}
Unless (open (OUT, ">>$ bo") {print "\ n [Warn] Can not open the output file <$ bo >:$! "; Exit}
}
Else {print "\ n [Warn] Can not open the output directory <$ dbo >:$! "; Exit}
}
}
Else {print "\ n [Warn] Can not open the output file <$ bo >:$! "; Exit}
}
Print OUT "$ cont ";
Close OUT;
}
Sub pmkpath
{
My @ p = split (/\\/, shift );
My $ pa = $ p [0];
My $ m =$ # p + 1;
My $ t;
For ($ t = 1;-e $ pa; $ t ++) {$ pa. = '\'. $ p [$ t]}
Unless (mkdir $ pa) {return 0}
For (; $ t <$ m; $ t ++)
{
$ Pa. = '\'. $ p [$ t];
Unless (mkdir $ pa) {return 0}
}
Return 1
}
Sub c0000t
{
Unless ($ response t) {return $ replacemen}
My $ con;
($ Con) = @_;
If ($ con = ~ /$ Excep/) {return $ con} else {return $ replacemen}
}
Download the packaged exe file