Batch script to find and replace a string in text file within a minute for files up to 12 MB

前端 未结 6 877
逝去的感伤
逝去的感伤 2020-12-02 10:13

I have written a batch script to replace a string in text file.

Following is the script.

@echo off &setlocal
set \"search=%1\"
set \"replace=%2\"         


        
6条回答
  •  情话喂你
    2020-12-02 10:54

    Give this a shot:

    @echo off
    setlocal
    
    call :FindReplace "findstr" "replacestr" input.txt
    
    exit /b 
    
    :FindReplace   
    set tmp="%temp%\tmp.txt"
    If not exist %temp%\_.vbs call :MakeReplace
    for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
      for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
        echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
        <%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
        if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
      )
    )
    del %temp%\_.vbs
    exit /b
    
    :MakeReplace
    >%temp%\_.vbs echo with Wscript
    >>%temp%\_.vbs echo set args=.arguments
    >>%temp%\_.vbs echo .StdOut.Write _
    >>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
    >>%temp%\_.vbs echo end with
    

提交回复
热议问题