0

我编写了这个脚本来替换文件中的行,但这需要很长时间。有没有办法加快速度?

@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=update.ver
set OUTTEXTFILE=update.txt
set SEARCHTEXT=93.184.71.27
set REPLACETEXT=93.184.72.27
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
    SET string=%%A
    setlocal EnableDelayedExpansion
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

    >> %OUTTEXTFILE% echo(!modified!
    endlocal
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%





@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=update.ver
set OUTTEXTFILE=update.txt
set SEARCHTEXT=91.228.167.26
set REPLACETEXT=91.227.167.26
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
    SET string=%%A
    setlocal EnableDelayedExpansion
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

    >> %OUTTEXTFILE% echo(!modified!
    endlocal
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%





@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=update.ver
set OUTTEXTFILE=update.txt
set SEARCHTEXT=91.228.166.13
set REPLACETEXT=91.227.166.13
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
    SET string=%%A
    setlocal EnableDelayedExpansion
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

    >> %OUTTEXTFILE% echo(!modified!
    endlocal
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%





@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=update.ver
set OUTTEXTFILE=update.txt
set SEARCHTEXT=91.228.166.15
set REPLACETEXT=91.227.166.15
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
    SET string=%%A
    setlocal EnableDelayedExpansion
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

    >> %OUTTEXTFILE% echo(!modified!
    endlocal
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

请帮我

4

2 回答 2

2

您可以使用我的 FindRepl.bat 程序在文件的一次处理过程中实现多个字符串替换。例如:

@echo off
setlocal
set INTEXTFILE=update.ver
set OUTTEXTFILE=update.txt
set "SEARCHTEXT=93\.184\.71\.27|91\.228\.167\.26|91\.228\.166\.13|91\.228\.166\.15"
set "REPLACETEXT=93\.184\.72\.27|91\.227\.167\.26|91\.227\.166\.13|91\.227\.166\.15"
call FindRepl "%SEARCHTEXT%" /A "%REPLACETEXT%" < %INTEXTFILE% > %OUTTEXTFILE%
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

FindRepl 中的 /A 开关指示 Search 和 Replace 字符串都有多个值,由竖线分隔。

FindRepl.bat 是一个 Batch-JScript 混合程序,因此它的运行速度比任何纯 Batch 解决方案都要快;你不需要任何额外的东西来使用它,只需从这个站点下载它

于 2013-09-22T09:38:13.033 回答
1
@ECHO OFF &SETLOCAL

set "INTEXTFILE=update.ver"
set "OUTTEXTFILE=update.txt"
set "SEARCHTEXT1=93.184.71.27"
set "REPLACETEXT1=93.184.72.27"
set "SEARCHTEXT2=91.228.167.26"
set "REPLACETEXT2=91.227.167.26"
set "SEARCHTEXT3=91.228.166.13"
set "REPLACETEXT3=91.227.166.13"
set "SEARCHTEXT4=91.228.166.15"
set "REPLACETEXT4=91.227.166.15"

(for /f "delims=" %%A in ( 'type "%INTEXTFILE%"') do (
    SET "string=%%A"
    setlocal EnableDelayedExpansion
    SET "modified=!string:%SEARCHTEXT1%=%REPLACETEXT1%!"
    SET "modified=!modified:%SEARCHTEXT2%=%REPLACETEXT2%!"
    SET "modified=!modified:%SEARCHTEXT3%=%REPLACETEXT3%!"
    SET "modified=!modified:%SEARCHTEXT4%=%REPLACETEXT4%!"
    echo(!modified!
    endlocal
))>"%OUTTEXTFILE%"

move "%OUTTEXTFILE%" "%INTEXTFILE%"
于 2013-09-22T09:52:33.300 回答