Superb GSM, I'm happy to leave your vbscript solution here to help show how much quicker it'll work than a batch only solution. Because it edits a stream as SED would but is built-in it deserves its place in this thread
I just thought I'd clarify for anyone interested that the expected output should be:
CODE
I am the first line of this document.
I once stated, "I am the second line".
Text string added!
I demand to stay put! I don't wish to move!
Do you wish to stop & start notepad, just start it, or neither?
My email, <something@somedomain.ext>, is not working!
For the sake of keeping everything together, here's the SED and GAWK solutions
SED
CODE
@Echo off&Setlocal
:: Existing Filename [to add text to]
Set "EF=existing.txt"
:: Line Number [to insert string]
Set "LN=3"
:: Text String [to add]
Set "TS=Text string added!"
:: New Filename [as updated document]
Set "NF=NewFile.ext"
::Type Nul>%NF%
Set/a PL=LN-1
Sed -n 1,%PL%p %EF%>%NF%
Echo:%TS%>>%NF%
Sed -n %LN%,$p %EF%>>%NF%
GAWK
CODE
@Echo off&Setlocal
:: Existing Filename [to add text to]
Set "EF=existing.txt"
:: Line Number [to insert string]
Set "LN=3"
:: Text String [to add]
Set "TS=Text string added!"
:: New Filename [as updated document]
Set "NF=NewFile.ext
::Type Nul>%NF%
Set/a PL=LN-1
Gawk "{print;if(NR==%PL%)print"""%TS%"""}" %EF%>%NF%
Congratulations Scr1ptW1zard, we have a working entry according to the original specification.
I have nothing but admiration for you for giving this a shot, was intrigued by your approach and love your use of the copy switch! I may be completely wrong but I have no recollection of seeing a script attempt this 'simple' task anywhere on the net and didn't expect anyone to take up the 'pointless' challenge. I'm however a little bit disappointed with one particular aspect; It seems as if it produces the output quicker than mine, the difference isn't much but I'm sure that if I was adding a line to a large file mine would be a lot slower.
Now for the kicker, to show how different content can really affect your work, change the insert line number to 13 and change the existing.txt to:
CODE
Column 1 | Column 2
A variable is enclosed in percent signs: %var1%
10% of 42 = 5% of 21
My email, <something@somedomain.ext>, is not working!
Do you wish to stop & start notepad, just start it, or neither?
I demand to stay put! I don't wish to move!
I stated, "I am in the middle of something".
I stated, "I am in the middle of something".
I demand to stay put! I don't wish to move!
Do you wish to stop & start notepad, just start it, or neither?
My email, <something@somedomain.ext>, is not working!
10% of 42 = 5% of 21
A variable is enclosed in percent signs: %var1%
Column 1 | Column 2
LinesNotesThis file is mirrored, first and last lines say <TAB>Column 1<TAB>|<TAB>Column 2
Lines 7 and 18 contain a single space
Lines 8 and 17 end with a trailing space
Now start pulling your hair out!
BTW, my solution adds just a single line to my 'starter' script, which I split, for beautifying reasons, into three
P.S I'm going to edit/append posts etc. in order not to completely ruin the original intent of the topic and try to keep only the better attempts here!