用批处理文件替换update.ver中的文本

时间:2013-09-22 07:35:18

标签: batch-file

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

@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%

请帮帮我

2 个答案:

答案 0 :(得分: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开关表示搜索和替换字符串都有多个由竖线分隔的值。

FindRepl.bat是一个Batch-JScript混合程序,因此运行速度比任何纯Batch解决方案快得多;您无需任何其他内容即可使用它,只需从this site

下载即可

答案 1 :(得分: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%"