Home > Enterprise >  How do I add a non-fixed line after a certain line of text in a text file using Batch?
How do I add a non-fixed line after a certain line of text in a text file using Batch?

Time:12-28

I am trying to automate adding a certain line of text into a text file. For example, adding this line "Script#3=script3" under the last row of the "~block 2~" paragraph. However, the amount of items in the "~block 2~" paragraph may vary.

"~block 1~"

Script#1=script1

Script#2=script2

"~block 2~"

Script#1=script1

Script#2=script2

"~block 3~"

Script#1=script1

Script#2=script2

TO

"~block 1~"

Script#1=script1

Script#2=script2

"~block 2~"

Script#1=script1

Script#2=script2

Script#3=script3

"~block 3~"

Script#1=script1

Script#2=script2

I have tried writing the code but it does not seem to be working.

@echo OFF

SET /A CHECK = 0
SET /A a = 1
(
FOR /F "tokens=*" %%A IN (C:\Users\Desktop\testBatchtestModify.txt) DO (
   ECHO %%A

   IF %CHECK%==0 (
    ECHO %%A
   )
   ELSE (
    IF "%%A" EQU "" (
       ECHO "Script#" %CHECK% "=TEST"
       ECHO 
       SET /A CHECK = 0
    )
    ELSE (
       SET /A CHECK = %CHECK% %a%
       ECHO %%A
    )
   )

   IF "%%A" EQU "~block 2~" (
    SET /A CHECK = 1
   )
) >> temp.txt
move /y temp.txt C:\Users\Desktop\testBatchtestModify.txt

CodePudding user response:

A working batch file for this line inserting task for the posted example with a text file containing empty lines and lines with " and ~ is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "TextFile=C:\Users\Desktop\testBatchtestModify.txt"
if not exist "%TextFile%" exit /B
set "LinesInserted="
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%TextFile%" 2^>nul') do (
    set "Line=%%I"
    setlocal EnableDelayedExpansion
    if not "!Line:block 3=!" == "!Line!" echo Script#3=script3& echo(& set "LinesInserted=1"
    echo(!Line:*:=!
    endlocal
))>"%TextFile%.tmp"
if defined LinesInserted move /Y "%TextFile%.tmp" "%TextFile%" >nul
if exist "%TextFile%.tmp" del "%TextFile%.tmp"
endlocal

For the main FOR loop see How to read and print contents of text file line by line?

Each line read from the file is also output unmodified and written into a temporary file opened once at before running the FOR loop and closed once after all lines are written into the file.

The IF condition checks if the current line contains case-insensitive the string block 3 in which case is output a line with Script#3=script3 and an empty line before the line with block 3.

The temporary file with the additional file replaces the original file if there are inserted indeed the two lines. Otherwise the temporary file being identical to the text file is deleted by the batch file.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • move /?
  • set /?
  • setlocal /?

CodePudding user response:

@ECHO OFF
SETLOCAL
rem The following settings for the source directories and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q74927660.txt"
SET "outfile=           
  • Related