1

我必须用一些特定的扩展名替换所有文件中的一些内容。

例如:在 toChange 文件夹中我可能有多个子文件夹,现在我想在所有带有扩展名的文件中isabcd更改isxyz文本html

此类命令的批处理是什么,我正在使用 Windows

实际上,我需要更换多个项目。我有它们的键值形式,需要在文件夹中的所有文件中一一替换

我的文字将有双引号,我想替换我的文字如下

if (loUserAgent.toLowerCase().indexOf("firefox") > -1
|| (loUserAgent.toLowerCase().indexOf("msie") > -1 && loUserAgent.toLowerCase().indexOf("msie 7") != -1))

我希望它被转换为

if (loUserAgent.toLowerCase().indexOf(aaa.FIRE_FOX) > -1
|| (loUserAgent.toLowerCase().indexOf(aaa.MSIE) > -1 && loUserAgent.toLowerCase().indexOf(aaa.IE_7) != -1))

conf.txt 文件将包含如下输入

 AAA.PY_ERROR_OCCURRED_WHILE_GETTING_PAYMENT_LIST_SCREEN_FOR_ORG_TYPE:::"Error occurred while getting payment list screen for org type: "
AAA.PY_AO_PAYMENT_LIST_SIZE:::"aoPaymentListSize"
AAA.PY_AO_PAYMENT_STATUS:::"aoPaymentStatus"

其中右侧:::的文本是需要替换的文本(带双引号),并且该文本也可能包含特殊字符,左侧的字符串是我希望用它替换的文本

PS:很抱歉询问直接解决方案。但是我不知道批处理文件:(

4

3 回答 3

2
@echo off
setlocal DisableDelayedExpansion

rem Load the replacement table from conf.txt file
set n=0
for /F "usebackq delims=" %%a in ("%~P0conf.txt") do (
   set "line=%%a"
   set /A n+=1
   setlocal EnableDelayedExpansion
   rem Use the first special character here, after second equal-sign and 15 characters forward
   rem and the second special character after the third equal-sign
   for /F "tokens=1,2 delims=Ç" %%b in ("!n!Ç!line::::=ü!") do (
      endlocal
      set "replace[%%b]=%%c"
   )
)

rem Process all HTML files in toChange folder
setlocal DisableDelayedExpansion
cd "C:\toChange"
for /R %%a in (*.html) do (
   rem Process all lines of this file
   (for /F "usebackq delims=" %%b in ("%%a") do (
      set "line=%%b"
      setlocal EnableDelayedExpansion
      rem Make the replacements
      for /L %%i in (1,1,%n%) do (
         rem Use the second special character here, after second equal-sign
         for /F "tokens=1,2 delims=ü" %%c in ("!replace[%%i]!") do (
            set "line=!line:%%d=%%c!"
         )
      )
      echo !line!
      endlocal
   )) > "%%a.txt"
   rem Remove the REM in next lines to delete original .html file and rename the created one
   REM del "%%a"
   REM ren "%%a.txt" "%%~NXa"
)

上面的批处理程序在与原始文件同名并添加“.txt”扩展名的文件中生成其输出。如果结果正确,请删除批处理文件末尾的 REM 命令以删除原始 .hmtl 文件并重命名创建的文件。我用这些数据测试了程序:

配置文件.txt

AAA.PY_ERROR_OCCURRED_WHILE_GETTING_PAYMENT_LIST_SCREEN_FOR_ORG_TYPE:::"Error occurred while getting payment list screen for org type: "
AAA.PY_AO_PAYMENT_LIST_SIZE:::"aoPaymentListSize"
AAA.PY_AO_PAYMENT_STATUS:::"aoPaymentStatus"
aaa.FIRE_FOX:::"firefox"
aaa.MSIE:::"msie"
aaa.IE_7:::"msie 7"

例子.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- Example HTML file -->

Normal text. <b>Bold text.</b> <i>Italic text.</i>

if (loUserAgent.toLowerCase().indexOf("firefox") > -1
|| (loUserAgent.toLowerCase().indexOf("msie") > -1 && loUserAgent.toLowerCase().indexOf("msie 7") != -1))

输出:example.html.txt

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- Example HTML file -->
Normal text. <b>Bold text.</b> <i>Italic text.</i>
if (loUserAgent.toLowerCase().indexOf(aaa.FIRE_FOX) > -1
|| (loUserAgent.toLowerCase().indexOf(aaa.MSIE) > -1 && loUserAgent.toLowerCase().indexOf(aaa.IE_7) != -1))

conf.txt 文件必须与批处理文件位于同一文件夹中。

该程序从输入文件中删除空行。如果您愿意,这可能很容易解决。

该程序可能有点慢。这可能会在某种程度上得到改进,但需要进行大量修改。

编辑:我修改了该程序的先前版本,以便:

  • 正确处理 conf.txt 文件中的感叹号。
  • 使用特殊字符来分隔 conf.txt 文件的替换值,而不是等号。

请注意,您可以在自己的文本中使用字符组合作为分隔符,例如:::,但在某些批处理管理中,分隔符必须只有一个字符。这个细节迫使我们选择一个不能出现在 conf.txt 文件中的字符。我的程序需要使用两个不同的特殊字符作为分隔符。我选择了 Ascii 字符 128 (Ç) 和 129 (ü);如果这些字符可能出现在 conf.txt 文件中,那么您必须选择另一个字符并在程序中的指定点更改它们。抱歉,没有其他方法可以在批处理文件中执行此操作。

于 2013-04-13T16:17:49.607 回答
2

我编写了一个名为 REPL.BAT 的混合 JScript/批处理实用程序,可以帮助解决这个问题。我相信它会比任何纯批处理解决方案都要快得多。另一个不错的功能是这个解决方案可以很容易地适应进行正则表达式搜索和替换,而不是字符串文字搜索和替换。

假设我的 REPL.BAT 和您的 conf.txt 与您的 html 文件位于同一文件夹中,那么以下脚本应该可以工作:

@echo off
setlocal disableDelayedExpansion

:: Define LF to contain a newline (0x0A) character
set LF=^


:: The 2 blank lines above are critical - DO NOT REMOVE

:: Read the search and replace strings and store them in an "array"
:: Also, create a variable containing a cascading set of piped commands to
:: carry out each search and replace
:: Normally the search is in the s array, and the replacement in the r array.
:: But if the replacement is an empty string, then the search is in the r array
:: and the s array value is undefined.
set "cnt=0"
set "update="
for /f delims^=^ eol^= %%A in (conf.txt) do (
  set "ln=%%A"
  set /a cnt+=1
  setlocal enableDelayedExpansion
  for %%n in ("!LF!") do set "ln=!ln::::=%%~n!"
  for %%N in (!cnt!) do (
    for /f delims^=^ eol^= %%U in (""!update!"") do (
      for /f delims^=^ eol^= %%L in ("!ln!") do (
        if "!"=="" endlocal
        if not defined r%%N (
          set "r%%N=%%L"
        ) else (
          set "s%%N=%%L"
        )
      )
      if "!"=="" endlocal
      if defined s%%N (
        set "update=%%~U|repl s%%N r%%N vl"
      ) else if defined r%%N (
        set "update=%%~U|repl r%%N "" vl"
      )
    )
  )
)

:: Process each file
for %%F in (*.html) do (
  echo processing %%F
  type "%%F" %update% >"%%F.new"
  move /y "%%F.new" "%%F" >nul
)
echo Done!

该脚本构建了一个搜索和替换字符串的“数组”:s1,s2,...snr1,r2,...rn

它还构建了一个update看起来像的命令

|repl s1 r1 vl|repl s2 r2 vl...|repl sn rn vl

每个文件都通过一组管道 REPL 命令传递,这些命令执行搜索和替换,每个 REPL 命令一个。

上面的脚本有以下限制:

  • 我构建的最终update命令的长度必须小于 8191 个字符,因此一次可以执行的搜索和替换操作的数量是有限的。理论上的限制是大约 480 次替换,但我不知道 Windows 在一个命令中处理这么多管道时会如何表现。
  • 任何搜索或替换字符串都不能包含:::. 这是 conf.txt 文件设计所固有的

这是上述脚本所需的 REPL.BAT 文件。完整的文档嵌入在脚本中。上面的脚本使用V从变量中读取搜索和替换字符串的L选项,以及强制进行文字搜索而不是正则表达式搜索的选项。

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::************ Documentation ***********
:::
:::REPL  Search  Replace  [Options  [SourceVar]]
:::REPL  /?
:::
:::  Performs a global search and replace operation on each line of input from
:::  stdin and prints the result to stdout.
:::
:::  Each parameter may be optionally enclosed by double quotes. The double
:::  quotes are not considered part of the argument. The quotes are required
:::  if the parameter contains a batch token delimiter like space, tab, comma,
:::  semicolon. The quotes should also be used if the argument contains a
:::  batch special character like &, |, etc. so that the special character
:::  does not need to be escaped with ^.
:::
:::  If called with a single argument of /? then prints help documentation
:::  to stdout.
:::
:::  Search  - By default this is a case sensitive JScript (ECMA) regular
:::            expression expressed as a string.
:::
:::            JScript regex syntax documentation is available at
:::            http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx
:::
:::  Replace - By default this is the string to be used as a replacement for
:::            each found search expression. Full support is provided for
:::            substituion patterns available to the JScript replace method.
:::            A $ literal can be escaped as $$. An empty replacement string
:::            must be represented as "".
:::
:::            Replace substitution pattern syntax is documented at
:::            http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx
:::
:::  Options - An optional string of characters used to alter the behavior
:::            of REPL. The option characters are case insensitive, and may
:::            appear in any order.
:::
:::            I - Makes the search case-insensitive.
:::
:::            L - The Search is treated as a string literal instead of a
:::                regular expression. Also, all $ found in Replace are
:::                treated as $ literals.
:::
:::            B - The Search must match the beginning of a line.
:::                Mostly used with literal searches.
:::
:::            E - The Search must match the end of a line.
:::                Mostly used with literal searches.
:::
:::            V - Search and Replace represent the name of environment
:::                variables that contain the respective values. An undefined
:::                variable is treated as an empty string.
:::
:::            M - Multi-line mode. The entire contents of stdin is read and
:::                processed in one pass instead of line by line. ^ anchors
:::                the beginning of a line and $ anchors the end of a line.
:::
:::            X - Enables extended substitution pattern syntax with support
:::                for the following escape sequences:
:::
:::                \\     -  Backslash
:::                \b     -  Backspace
:::                \f     -  Formfeed
:::                \n     -  Newline
:::                \r     -  Carriage Return
:::                \t     -  Horizontal Tab
:::                \v     -  Vertical Tab
:::                \xnn   -  Ascii (Latin 1) character expressed as 2 hex digits
:::                \unnnn -  Unicode character expressed as 4 hex digits
:::
:::                Escape sequences are supported even when the L option is used.
:::
:::            S - The source is read from an environment variable instead of
:::                from stdin. The name of the source environment variable is
:::                specified in the next argument after the option string.
:::

::************ Batch portion ***********
@echo off
if .%2 equ . (
  if "%~1" equ "/?" (
    findstr "^:::" "%~f0" | cscript //E:JScript //nologo "%~f0" "^:::" ""
    exit /b 0
  ) else (
    call :err "Insufficient arguments"
    exit /b 1
  )
)
echo(%~3|findstr /i "[^SMILEBVX]" >nul && (
  call :err "Invalid option(s)"
  exit /b 1
)
cscript //E:JScript //nologo "%~f0" %*
exit /b 0

:err
>&2 echo ERROR: %~1. Use REPL /? to get help.
exit /b

************* JScript portion **********/
var env=WScript.CreateObject("WScript.Shell").Environment("Process");
var args=WScript.Arguments;
var search=args.Item(0);
var replace=args.Item(1);
var options="g";
if (args.length>2) {
  options+=args.Item(2).toLowerCase();
}
var multi=(options.indexOf("m")>=0);
var srcVar=(options.indexOf("s")>=0);
if (srcVar) {
  options=options.replace(/s/g,"");
}
if (options.indexOf("v")>=0) {
  options=options.replace(/v/g,"");
  search=env(search);
  replace=env(replace);
}
if (options.indexOf("l")>=0) {
  options=options.replace(/l/g,"");
  search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1");
  replace=replace.replace(/\$/g,"$$$$");
}
if (options.indexOf("b")>=0) {
  options=options.replace(/b/g,"");
  search="^"+search
}
if (options.indexOf("e")>=0) {
  options=options.replace(/e/g,"");
  search=search+"$"
}
if (options.indexOf("x")>=0) {
  options=options.replace(/x/g,"");
  replace=replace.replace(/\\\\/g,"\\B");
  replace=replace.replace(/\\b/g,"\b");
  replace=replace.replace(/\\f/g,"\f");
  replace=replace.replace(/\\n/g,"\n");
  replace=replace.replace(/\\r/g,"\r");
  replace=replace.replace(/\\t/g,"\t");
  replace=replace.replace(/\\v/g,"\v");
  replace=replace.replace(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}/g,
    function($0,$1,$2){
      return String.fromCharCode(parseInt("0x"+$0.substring(2)));
    }
  );
  replace=replace.replace(/\\B/g,"\\");
}
var search=new RegExp(search,options);

if (srcVar) {
  WScript.Stdout.Write(env(args.Item(3)).replace(search,replace));
} else {
  while (!WScript.StdIn.AtEndOfStream) {
    if (multi) {
      WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(search,replace));
    } else {
      WScript.Stdout.WriteLine(WScript.StdIn.ReadLine().replace(search,replace));
    }
  }
}
于 2013-04-14T18:38:05.093 回答
0

启动mintty.exe(您的 cygwin 终端)并执行以下命令。酌情修改/cygdrive/c/Users/etc

find /cygdrive/c/Users/Varun/Documents/etc/toChange -type f -iname '*.html' -print0 | xargs -0 sed -i -r -e "/aaa\.[A-Z1-9_]+\s*=/!s/\Wfirefox\W/aaa.FIRE_FOX/g" -e "/aaa\.[A-Z1-9_]+\s*=/!s/\Wmsie 7\W/aaa.IE_7/g" -e "/aaa\.[A-Z1-9_]+\s*=/!s/\Wmsie\W/aaa.MSIE/g"

这意味着:

  • /path/to/toChange
  • 查找f带有.html扩展名的文件(不是目录)
  • 将每个匹配项传递给sed,这将修改内联内容。
  • 对于每行包含var aaa.FIRE_FOX =或类似的,
    • 全局搜索[non alpha-numeric]firefox[non alpha-numeric]
    • 并替换为aaa.FIRE_FOX

... 等等。

test.html之前:

var aaa.FIRE_FOX="firefox"
var aaa.MSIE="msie"
var aaa.IE_7="msie 7"
if (loUserAgent.toLowerCase().indexOf("firefox") > -1
|| (loUserAgent.toLowerCase().indexOf("msie") > -1 && loUserAgent.toLowerCase().indexOf("msie 7") != -1))

命令后的test.htmlfind | xargs sed

var aaa.FIRE_FOX="firefox"
var aaa.MSIE="msie"
var aaa.IE_7="msie 7"
if (loUserAgent.toLowerCase().indexOf(aaa.FIRE_FOX) > -1
|| (loUserAgent.toLowerCase().indexOf(aaa.MSIE) > -1 && loUserAgent.toLowerCase().indexOf(aaa.IE_7) != -1))

如果这对您不起作用,也许 Endoro 建议使用带有 GUI 的东西(例如 WinGrep)可能会更好

于 2013-04-11T18:19:56.650 回答