batch file inserting new lines before a string in text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
karlo
Posts: 2
Joined: 20 Jun 2014 09:22

batch file inserting new lines before a string in text file

#1 Post by karlo » 20 Jun 2014 09:26

need your help to develop a batch file in order to read / find a string in a text file and insert a new line "before" it;

for instants, change the following text to (insert "R 100" before Z10)

a 1
b 2
c 3
z 10
q 15

a 1
b 2
c 3
R 100
z 10
q 15
=============================================================================

i found a similar code and tried to modify it for my need. but it doesn't work!! could you give me a hint why the following code is not working !!!

Code: Select all

@echo off
 setlocal

set inputfile=C:\Users\Desktop\test\file1.txt
set outputfile=C:\Users\Desktop\test\file2.txt
    set "start="
    for /f usebackq^ delims^=^ eol^=  %%a in ("%inputfile%") do (
        if not defined start set "start=1" & break > "%inputfile%"
        if "%%~a"=="z 10" >>"%inputfile%" echo( R 100)

       )>>"%outputfile%" echo(%%a)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file inserting new lines before a string in text

#2 Post by foxidrive » 20 Jun 2014 10:17

Does this work for you?

Code: Select all

@echo off
set inputfile=C:\Users\Desktop\test\file1.txt
set outputfile=C:\Users\Desktop\test\file2.txt

    for /f "usebackq delims="  %%a in ("%inputfile%") do (
          if "%%~a"=="z 10" >>"%outputfile%" echo(R 100
          >>"%outputfile%" echo(%%a
    )
pause

karlo
Posts: 2
Joined: 20 Jun 2014 09:22

Re: batch file inserting new lines before a string in text

#3 Post by karlo » 20 Jun 2014 11:24

foxidrive, you are amazing! thanks. yes, it works.

newprintm
Posts: 3
Joined: 24 Sep 2020 11:18

Re: batch file inserting new lines before a string in text file

#4 Post by newprintm » 24 Sep 2020 11:25

Foxdrive, hi.

I am using the same code, but in my case ti need to add some lines after the founded string (word)

file1.txt, looks like:

;word in place
house
;word and tree
123
45

and the final result (file2.txt) after execute the .bat , must look like:

;word in place
NEW LINE 1
NEW LINE 2
house
;word and tree
NEW LINE 1
NEW LINE 2
123
45

I hope you can help me.
Best regards

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: batch file inserting new lines before a string in text file

#5 Post by Squashman » 25 Sep 2020 11:28

newprintm wrote:
24 Sep 2020 11:25
I am using the same code, but in my case ti need to add some lines after the founded string (word)
It is as simple as flipping the two lines of code. The if condition has to follow the output of the line of code you are testing.
So just reverse these two lines.

Code: Select all

if "%%~a"=="z 10" >>"%outputfile%" echo(R 100
>>"%outputfile%" echo(%%a

newprintm
Posts: 3
Joined: 24 Sep 2020 11:18

Re: batch file inserting new lines before a string in text file

#6 Post by newprintm » 25 Sep 2020 16:24

Hi Squashman,

i understand that you said.
But now the code works only if the founded word is exactly the same at all the txt line evaluated .

any tips to solve that?

thanks in advance

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch file inserting new lines before a string in text file

#7 Post by aGerman » 26 Sep 2020 08:58

Always depends on how the content of your file actually looks like.

Code: Select all

@echo off &setlocal
set "inputfile=file1.txt
set "outputfile=file2.txt"
set "find=;word "

setlocal EnableDelayedExpansion

<"!inputfile!" >"!outputfile!" (
  for /f %%i in ('type "!inputfile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "ln=" &set /p "ln="
    echo(!ln!
    if defined ln if "!ln:%find%=!" neq "!ln!" (
      echo NEW LINE 1
      echo NEW LINE 2
    )
  )
)
Steffen

newprintm
Posts: 3
Joined: 24 Sep 2020 11:18

Re: batch file inserting new lines before a string in text file

#8 Post by newprintm » 26 Sep 2020 10:56

You are the best!!, your code works perfectly.

thank you very much
regards
Miguel

Post Reply