computing
  • 1

Batch: Replace Specific Lines In a Txt File

  • 1

Hello, I am new to Batch scripting and am having trouble doing the following.
I would like to have a batch script that scans a set of batch files and makes a set of replacements to the text data. The replacements involve: commenting out some known lines (using ::), replacing a set of 4 lines another set of 7 lines.
Looking forward to your help!

Share

1 Answer

  1. If your 4 lines to replace are in rows 5 – 8 and the 7 newlines are in newseven.txt:
    ==============================================
    @echo off > newfile & setLocal enableDELAYedeXpansion

    for /f “tokens=1* delims=[]” %%a in (‘find /n /v “” ^< myfile’) do (
    if %%a leq 4 >> newfile echo.%%b
    if %%a equ 4 >> newfile type newseven.txt
    if %%a geq 9 >> newfile echo.%%b
    )

    =====================================
    Helping others achieve escape felicity

    M2

    • 0