Page 1 of 1

batch file inserting new lines before a string in text file

Posted: 20 Jun 2014 09:26
by karlo
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)

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

Posted: 20 Jun 2014 10:17
by foxidrive
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

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

Posted: 20 Jun 2014 11:24
by karlo
foxidrive, you are amazing! thanks. yes, it works.

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

Posted: 24 Sep 2020 11:25
by newprintm
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

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

Posted: 25 Sep 2020 11:28
by Squashman
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

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

Posted: 25 Sep 2020 16:24
by newprintm
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

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

Posted: 26 Sep 2020 08:58
by aGerman
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

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

Posted: 26 Sep 2020 10:56
by newprintm
You are the best!!, your code works perfectly.

thank you very much
regards
Miguel