1.

Solve : Echo + Append text to specific line in a txt file..???

Answer»

is there a way to echo + Append TEXT into a specific line in a .txt file..
like say i ALREADY have a txt file wit 10 lines of txt in it.
and i want to echo something in on line 5 but have it echoed in after all the txt on that line..
Ex:
.txt file
Code: [Select]1---:
2---:
3---:
4---:
5---: !!!..Echoed in txt here..!!!
6---:
7---:
8---:
9---:
10---:

can this be done at all with batch code..??You can do it .. sort of, you will need to read through the whole file, writing out what you have read, if the current line is the one you wish to change, then WRITE out the changed value

Grahamhow exactly do i do that..??Anyone KNOW how to do this..??Do you mean you already have a text file like this:

Quote from: Text File

1---:
2---:
3---:
4---:
5---:
6---:
7---:
8---:
9---:
10---:
And you want to add !!!..Echoed in txt here..!!! on the fifth line?yup, u got it, thas wat i wana do..
can it be done..??With the other tools available on modern machines, why did you pick a batch solution?

Code: [Select]@echo off
setlocal enabledelayedexpansion
set count=0
for /f %%i in (input.txt) do (
call set /a count=%%count%%+1
if !count!==5 (set line=%%i ..Echoed in txt here..) else (set line=%%i)
echo !line!>>output.txt
)

This comes dangerously close to programming and we all know batch coding is not a programming language.

Note: Exclamation marks (!) have special meaning in batch files. If you must absolutely have them, you'll need to work that part of the solution yourself.


thanks for the help..
but this seems to just duplicate the input.txt file into the output.txt then appends then new text into the output.txt..

I need it to append the new text to the 5th line on the original input.txt Quote
I need it to append the new text to the 5th line on the original input.txt

That is not going to happen. Batch code can only process sequential files. Sequential files cannot be updated in place (cannot be opened both input and output at the same time). If the new record were larger than the old record, how would it fit into the old space?

Here is one workaround:
Code: [Select]@echo off
setlocal enabledelayedexpansion
set count=0
for /f %%i in (input.txt) do (
call set /a count=%%count%%+1
if !count!==5 (set line=%%i ..Echoed in txt here..) else (set line=%%i)
echo !line!>>output.txt
)
ren input.txt input.old
ren output.txt input.txt

I dislike writing destructive code but the first ren command can be replaced by a del command.



Discussion

No Comment Found