0

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

  • 1
    `ELSE (` should not be on a line on its own. There is no such command as `ELSE`, it is simply an option for the `IF` command. Please merge line 11 with 12 and line 17 with 18. – Compo Dec 27 '22 at 11:27

3 Answers3

0

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
(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(
    echo(!Line:*:=!
    endlocal
))>"%TextFile%.tmp"
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 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 lines replaces the original 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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
0
@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=%destdir%\outfile.txt"

SET "insertme=Script#3=script3"
SET "atendofblock=~block 2~"
SET "blockstart="

(
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
 IF "%%~e" neq "%%e" (
  IF DEFINED blockstart IF DEFINED insertme (ECHO %insertme%&SET "insertme=")
  SET "blockstart="
 )
 IF "%%~e"=="%atendofblock%" SET "blockstart=y"
 ECHO %%e
)
IF DEFINED insertme IF DEFINED blockstart ECHO %insertme%&SET "insertme="
)>"%outfile%"

IF DEFINED insertme ECHO "%atendofblock%" NOT found - no CHANGE made

GOTO :EOF

Always verify against a test directory before applying to real data.

Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.

Read each line of the file.

If the line matches the at-end-of-block name, set blockstart to something (it becomes defined)

If the line is quoted then we have a new block, so if blockstart is set, output the "insert" line and set blockstart to nothing (blockstart becomes undefined)

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

This simpler method does not insert the line at end of block 2, but before block 3 ;)

@echo off
setlocal EnableDelayedExpansion

for /F "delims=:" %%a in ('findstr /N /L /C:"~block 3~" test.txt') do set /A "lines=%%a-1"

< test.txt (
   for /L %%a in (1,1,%lines%) do (
      set "line="
      set /P "line="
      echo(!line!
   )
   echo Script#3=script3
   echo/
   findstr "^"
) > temp.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108