Batch file 是否使用批处理脚本替换文件第7行中的文本?

Batch file 是否使用批处理脚本替换文件第7行中的文本?,batch-file,Batch File,我有一个文件,第7行声明了一个变量 版本=6.0.196.0。我需要将此行编辑为VERSION=6.0.200.0 第15行为MANIFEST_BUILD=>196。这应该更改为MANIFEST_BUILD=>200。 如何做到这一点?此答案中的所有解决方案仅使用Windows XP及更高版本的本机命令 1) 这是迄今为止最快的本机批处理解决方案,但它有最多的限制 文件中前15行的长度都不能超过1021字节 所有前15行必须以(Windows样式)而不是(unix样式)结尾 前15行中没有一行

我有一个文件,第7行声明了一个变量 版本=6.0.196.0。我需要将此行编辑为VERSION=6.0.200.0

第15行为MANIFEST_BUILD=>196。这应该更改为MANIFEST_BUILD=>200。
如何做到这一点?

此答案中的所有解决方案仅使用Windows XP及更高版本的本机命令

1) 这是迄今为止最快的本机批处理解决方案,但它有最多的限制

  • 文件中前15行的长度都不能超过1021字节
  • 所有前15行必须以
    (Windows样式)而不是
    (unix样式)结尾
  • 前15行中没有一行可以以控制字符结尾(当然行终止符除外)
XP和Windows 7还有一个附加限制:文件的最后一行必须以
结尾。如果由于FINDSTR中的错误,最后一行没有以
结尾,脚本将无限期挂起。有关更多信息,请参阅

@echo off
setlocal enableDelayedExpansion
set "file=test.txt"

REM redirect input from your file and output to a new file
<"%file%" >"%file%.new" (

  REM preserve lines 1-6
  for /l %%N in (1 1 6) do (
    set "ln="
    set /p "ln="
    echo(!ln!
  )

  REM replace the next line (7)
  set /p "ln="
  echo VERSION=6.0.196.0

  REM preserve lines 8-14
  for /l %%N in (8 1 14) do (
    set "ln="
    set /p "ln="
    echo(!ln!
  )

  REM replace the next line (15)
  set /p "ln="
  REM The > below must be escaped
  echo MANIFEST_BUILD =^>200

  REM preserve the remaining lines
  findstr "^"

)

REM replace your original file with the new file
>nul move /y "%file%.new" "%file%"

3)此选项与选项2几乎相同,只是它允许行以
开头。它甚至比选项2)更慢


4)最后一个选项是迄今为止最稳健的。它使用我编写的混合JScript/批处理实用程序脚本,可用于处理文本文件。该脚本仍然只使用所有现代Windows平台(XP以后)固有的命令

此选项比选项1慢,但它可以在合理的时间内处理非常大的文件。它比选项2和3快得多,后者对于大文件来说太慢了

此选项还保留原始文件中存在的行终止符

我不知道这个选项有什么限制

下面是依赖于REPL.BAT实用程序的脚本

@echo off
set "file=test.txt"
findstr /n "^" "%file%" | repl "^(7:.*)" "VERSION=6.0.196.0" m | repl "^(15:.*)" "MANIFEST_BUILD =>200" m | repl "^\d*:" "" m>"%file%.new"
>nul move /y "%file%.new" "%file%"
下面是上面的脚本所需的REPL.BAT实用程序。REPL.BAT应放置在路径中的位置。脚本中嵌入了完整的文档

@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 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.
:::
:::            E - 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 "[^SMILEX]" >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("e")>=0) {
  options=options.replace(/e/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("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));
    }
  }
}

此答案中的所有解决方案仅使用Windows XP及更高版本的本机命令

1) 这是迄今为止最快的本机批处理解决方案,但它有最多的限制

  • 文件中前15行的长度都不能超过1021字节
  • 所有前15行必须以
    (Windows样式)而不是
    (unix样式)结尾
  • 前15行中没有一行可以以控制字符结尾(当然行终止符除外)
XP和Windows 7还有一个附加限制:文件的最后一行必须以
结尾。如果由于FINDSTR中的错误,最后一行没有以
结尾,脚本将无限期挂起。有关更多信息,请参阅

@echo off
setlocal enableDelayedExpansion
set "file=test.txt"

REM redirect input from your file and output to a new file
<"%file%" >"%file%.new" (

  REM preserve lines 1-6
  for /l %%N in (1 1 6) do (
    set "ln="
    set /p "ln="
    echo(!ln!
  )

  REM replace the next line (7)
  set /p "ln="
  echo VERSION=6.0.196.0

  REM preserve lines 8-14
  for /l %%N in (8 1 14) do (
    set "ln="
    set /p "ln="
    echo(!ln!
  )

  REM replace the next line (15)
  set /p "ln="
  REM The > below must be escaped
  echo MANIFEST_BUILD =^>200

  REM preserve the remaining lines
  findstr "^"

)

REM replace your original file with the new file
>nul move /y "%file%.new" "%file%"

3)此选项与选项2几乎相同,只是它允许行以
开头。它甚至比选项2)更慢


4)最后一个选项是迄今为止最稳健的。它使用我编写的混合JScript/批处理实用程序脚本,可用于处理文本文件。该脚本仍然只使用所有现代Windows平台(XP以后)固有的命令

此选项比选项1慢,但它可以在合理的时间内处理非常大的文件。它比选项2和3快得多,后者对于大文件来说太慢了

此选项还保留原始文件中存在的行终止符

我不知道这个选项有什么限制

下面是依赖于REPL.BAT实用程序的脚本

@echo off
set "file=test.txt"
findstr /n "^" "%file%" | repl "^(7:.*)" "VERSION=6.0.196.0" m | repl "^(15:.*)" "MANIFEST_BUILD =>200" m | repl "^\d*:" "" m>"%file%.new"
>nul move /y "%file%.new" "%file%"
下面是上面的脚本所需的REPL.BAT实用程序。REPL.BAT应放置在路径中的位置。脚本中嵌入了完整的文档

@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 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.
:::
:::            E - 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 "[^SMILEX]" >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("e")>=0) {
  options=options.replace(/e/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("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));
    }
  }
}

您已经得到了一个非常全面(令人印象深刻)的Windows本机答案。不过,如果您可以下载其他实用程序,我建议您尝试sed,因为您已经得到了一个非常全面(且令人印象深刻)的Windows本机答案。不过,如果您可以下载其他实用程序,我建议您试试sed