Batch file 替换.bat脚本中第一次出现的文本

Batch file 替换.bat脚本中第一次出现的文本,batch-file,Batch File,我需要替换第一次出现的您不能批量执行这些类型的(高级)字符串操作,您需要一种更强大的语言,例如编写.NET控制台应用程序,或使用vbscript或powershell之类的脚本语言。是的,您可以使用 @echo off set "infile=C:\temp\myFile.php" set "outfile=C:\temp\myOut.php" setlocal DisableDelayedExpansion set "first=1" ( for /f "delims=" %%A in

我需要替换第一次出现的
您不能批量执行这些类型的(高级)字符串操作,您需要一种更强大的语言,例如编写.NET控制台应用程序,或使用vbscript或powershell之类的脚本语言。

是的,您可以使用

@echo off
set "infile=C:\temp\myFile.php"
set "outfile=C:\temp\myOut.php"
setlocal DisableDelayedExpansion
set "first=1"
(
    for /f "delims=" %%A in ('"findstr /n ^^ %infile%"') do (
        set "line=%%A"
        setlocal EnableDelayedExpansion

        set "line=!line:*:=!"
        if defined first (
            if defined line (
                set "replace=!line:<?php=<?php this is a test!"
                if !line! NEQ !replace! (
                  set "first="
                  set "line=!replace!"
                )
            )
        )
        (echo(!line!)
        if not defined first (
            endlocal
            set "first="
        ) ELSE (
            endlocal
        )
    ) 
) > %outfile%
@echo关闭
设置“infle=C:\temp\myFile.php”
设置“outfile=C:\temp\myOut.php”
setlocal DisableDelayedExpansion
设置“first=1”
(
对于/f“delims=“%”中的%%A(“'findstr/n^^%infle%”)do(
设置“行=%%A”
setlocal EnableDelayedExpansion
设置“行=!行:*=!”
如果先定义(
如果定义行(

设置“替换=!行:文件操作在这里不是一个很普通的批次的优点。实际上没有任何有用的命令可以使用。我会考虑使用脚本语言来代替这一点。也许是PosikSu壳。+ 1经常对批处理文件所能做的感到惊讶……大概是<代码>(ECHO(!
构造可以防止包含类似
的行…它看起来不匹配
(…)
,但从一些测试来看,它看起来像
回声(
(没有空格)“混淆”了它,因此它既不会对
行中的任何
做出反应,也不会打印周围的
(…)
。必须记住!@tripehund
回声(
是键,它是。编码括号只是为了避免尾随空格