1.

Solve : Inserting lines of info between line entries?

Answer»

Wondering if there is a way to add info between lined entries in text file using batch. The problem I am thinking is that you need a reference point for the batch to find and then replace that reference point with the lines of info, but if there is no reference point that is unique to every line, what are my options?

Data is like the following:

BLUE
GREEN
RED
YELLOW

4 lines with info on them...now I want to place A, B, C, D between each line on its own line between every line entry. This might be too much for batch to handle??? Any suggestions?

Data in the end would be like the following reading in each line to EOF and adding the ABCD on their own lines:

BLUE
A
B
C
D
GREEN
A
B
C
D
RED
A
B
C
D
YELLOW
A
B
C
D

In the end the A,B,C, and D will be replaced with other info using simple find and replace, but getting to this point is a challenge and its not logical to type it in all by keyboard since I am looking at tens of thousands of line entries that all need the ABCD placed between them =(  , so I have to code up a way to do this and not sure where to start  =(

Was thinking about reading it all into a large array and then using a LOOP to step++ through each item in the array, placing the first line item into a new file, then place A,B,C,D on their own lines, then step to the 2nd array piece of data until EOF, but thought I'd check first to see if this can be done with batch before maybe over complicating this for myself with C++.
you could do it in python, something like this:

Code: [Select]#!/usr/bin/env python
import sys
elements=sys.argv.split(" ")
#open first file (elements[0])
firstfile=open(elements[0])
#open second file (elements[1])
secondfile=open(elements[1],"w")
for line in firstfile:
    secondfile.write(line + "\n")
    secondfile.write("A\nB\nC\nD\n")

secondfile.close()
firstfile.close()

Or in VBScript:

Code: [Select]'VBScript
Dim INPUTFILE,outputfile
Dim sinputfile,soutputfile
dim FSO
set FSO=CreateObject("Scripting.FileSystemObject")
sinputfile=WScript.Arguments(0)
soutputfile=WScript.Arguments(1)
set inputfile = FSO.OpenTextFile(sinputfile)
set outputfile=FSO.CreateTextFile(soutputfile)

Do Until inputfile.AtEndOfStream
'Read a line from the input file.
LineRead = inputfile.ReadLine()

'write to the output file.
outputfile.WriteLine LineRead
outputfile.WriteLine("A")
outputfile.WriteLine("B")
outputfile.WriteLine("C")
outputfile.WriteLine("D")
Loop
outputfile.Close()
inputfile.Close()

Both of those would take the input file as the first argument and the output file as the second.
Certainly no need to use C++.

Also, it's quite easy to do with batch:

Code: [Select]echo off
IF EXIST %2 (
echo %2 will be DELETED. Press Control-Break to cancel.
pause
)
del %2
For /F "tokens=*" %%A in (%1) do (
echo %%A
echo A
echo B
echo C
echo D
) >> %2

I think I may have misunderstood the question, as you seem to refer to "searching" for something, as well.
Thanks... looking at the batch, I am trying to figure out the read-in file and write-out file functions as I see in the Python and VB Script to open and close files to be used for reading and writing the results ?

I see the write to %2 and looks like read in of %1...do they have to be variables or can I simply place readfile.txt in place of %1 and writefile.txt in place of %2 to write the results directly to this file? I am very rusty when it comes to batch programming so I am trying to make sense out of this instead of just running it  =)

In reference to the question about searching, I think I was getting at that without a reference in the file to perform an insertion of info, I was under the assumption that all would have to be read into an array and then the output file would have to be created by writing the first piece of data from teh array, then A thru D on their own lines, and then write the 2nd piece of data from the array until all data was covered within  the array. Looking at your code, I can see I was overcomplicating the matters.So replacing %1 with file to read in and %2 with file to write out with A,B,C,D's works... COOL

Question I have now is did I just make it ugly by placing the filenames in directly or should I have set the filenames to the %1 and %2 variables instead as proper programming technique? ( saying this from a novice programmer who use to use lots of goto's in Basic and later finding out that its poor programming technique such as when I took C++ in college. Placing the file names in directly might be sort of like a goto habit or is it correct to replace and use direct vs assigning the filenames to variables %1 and %2?)This is a general reply.
Much has been said about programming style. The believe is that ordinary people can become good code writers it they have a structure that other people approve of.
If you already have a style that works for you, there is little reason change it. Or is there?

If you want to, you may wish to review the works of Daniel D. McCracken. He has been around a long time. He his, IMO, the foremost author on programming structure. But he does not need my humble opinion.
http://www.amazon.com/Daniel-D.-McCracken/e/B001IU2THS/ref=ntt_athr_dp_pel_pop_1




Quote from: DaveLembke on December 03, 2010, 07:16:27 PM

So replacing %1 with file to read in and %2 with file to write out with A,B,C,D's works... COOL

Question I have now is did I just make it ugly by placing the filenames in directly or should I have set the filenames to the %1 and %2 variables instead as proper programming technique? ( saying this from a novice programmer who use to use lots of goto's in Basic and later finding out that its poor programming technique such as when I took C++ in college. Placing the file names in directly might be sort of like a goto habit or is it correct to replace and use direct vs assigning the filenames to variables %1 and %2?)

%1 and %2 aren't variables... they are the replacable parameters. If you want you could replace them with something like %infile% and %outfile% (and set them when the batch starts).

The entire point is so you can change it easier later on. Would you rather change the filenames in one place or have to look through the batch to see every time you used it? Even if it's only used once, you'll still have to look through the file.

Geek-9pm: "Beautiful Code" and "Code Complete" are two of the best programming books about style/conventions. "The Mythical Man Month" has been the main book about project management, and of course applies to anything outside of programming/software development as well.Thanks for the link to those books.

Also thanks for clarifying the %1 and %2 replacable parameters. I remember working on a batch about a year ago that used SET (and a piece of string info) = %1 etc and so I assumed they were variables where the %1 assumed the string value etc of the string info before the = sign. So if they are replacable parameters then they are really pointers to the string I guess so as you mentioned you can make a change to your SET line for %1 and %2 and have it affect all instances of %1 and %2 like a variable initialization would at the beginning of a program as I am use to doing in other languages such as INT A=1; , but its not a variable. Quote from: BC_Programmer on December 03, 2010, 10:10:48 PM
Geek-9pm: "Beautiful Code" and "Code Complete" are two of the best programming books about style/conventions. "The Mythical Man Month" has been the main book about project management, and of course applies to anything outside of programming/software development as well.
That is not yet the worst recommendation you have made. You could have done worse. Maybe.
Quote
From:
Coding Horror
programming and human factors
by Jeff Atwood Feb 20, 2008

I was thrilled to see the book Beautiful Code: Leading Programmers EXPLAIN How They Think show up in my Amazon recommendations. It seems like exactly the type of book I would enjoy. So of course I bought a copy. 
Unfortunately, Beautiful Code wasn't nearly as enjoyable of a read as I had HOPED it would be. It is by no means a bad book, but there's something about it that's not quite right.

Part of the problem is that it's a compilation of disconnected essays, much like The Best Software Writing I. Because there are thirty-three different authors, there's naturally going to be a lot of variance in tone, content, and quality. How you feel about the book is largely dicated by how much you like the individual essays. There's certainly no lack of quality in the authors. There are plenty of famous, highly regarded programmers represented here: Brian Kernighan, Yukihiro Matsumoto, Jon Bentley, Charles Petzold, and many others.

......why can't I love Beautiful Code? I wasn't able to put my finger on exactly what the deeper problem was with Beautiful Code until I read this eloquent reader review from Dmitry Dvoinikov. I suddenly realized what ultimately trips up Beautiful Code. It was right there in front of me, all along. It's even in the title: Code.    .....
http://www.codinghorror.com/blog/2008/02/code-isnt-beautiful.html
That book would qualify for a punishment reading  for students that misbehave.
Quote from: DaveLembke on December 04, 2010, 02:23:25 PM
Also thanks for clarifying the %1 and %2 replacable parameters. I remember working on a batch about a year ago that used SET (and a piece of string info) = %1
the piece of code was wrong. it always SET =value. it was probably setting something to be the first argument to the batch.



Quote
So if they are replacable parameters then they are really pointers to the string I guess so as you mentioned you can make a change to your SET line for %1 and %2 and have it affect all instances of %1 and %2 like a variable initialization would at the beginning of a program as I am use to doing in other languages such as INT A=1; , but its not a variable.

Err... no... I'm pretty sure you can't change the parameters you've been passed.DaveLembke, I will try to provide a succinct account of replaceable parameters. In a batch file you can have (let's keep it simple for now) up to nine parameters %1 to %9. These are parameters which are passed to the batch from outside e.g. from the command line. They are read-only. White space is a delimiter. Suppose you have a batch called showme.bat as follows

Code: [Select]echo off
echo parameter 1 is %1
echo parameter 2 is %2
echo parameter 3 is %3
and you called it like this from the prompt or another script or program

Code: [Select]showme Merry Christmas Everybody
The result would be

Code: [Select]parameter 1 is Merry
parameter 2 is Christmas
parameter 3 is Everybody




%1 and %2 are command_line arguments....look at output* below
C:\test>type  dave2.bat
echo off

echo. > neworig.txt
For /F "delims=" %%i in (%1) do (
echo %%i
echo A
echo B
echo C
echo D
) >> %2
type %2

rem  "delims="  is better usage than "tokens=*"
rem use token when we need to examine each word in a line

*output:

C:\test> dave2.bat orig.txt  neworig.txt

BLUE
A
B
C
D
GREEN
A
B
C
D
RED
A
B
C
D
YELLOW
A
B
C
D
C:\test>



Discussion

No Comment Found