Adding a line from one text file to another text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
virad
Posts: 3
Joined: 15 Apr 2014 13:38

Adding a line from one text file to another text file

#1 Post by virad » 15 Apr 2014 13:49

Hey guys, I'm doing a project and the program I'm trying to use only reads the last line of the latest updated file.

So I have a two text files, a.txt and b.txt

a.txt has bunch of numbers
11
123
22
33

b.txt as well
44
99
4321
11

I'm trying to make a batch file to add the lines of a.txt to b.txt ONE LINE AT A TIME in certain time interval.
So I would like line 1 '11' to be added to the end of b.txt. Then after a bit a delay of couple seconds, add line 2 '123' of a.txt onto the last line of b.txt.

Making this into a new file is not a problem as long as the last file is updated.

I've seen the following from another post but I need it to update one line at a time. Please help :)
TYPE B.TXT>>A.TXT should append b.txt to a.txt (caps are for emphasis only)
Copy a.txt+b.txt requires a new output file, a file cannot be copied onto itself.
e.g. copy a.txt+b.txt c.txt (c.txt would contain the appended files.)

Thank you for the help!

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

Re: Adding a line from one text file to another text file

#2 Post by foxidrive » 15 Apr 2014 16:42

Test this:


Code: Select all

@echo off
for /f "usebackq delims=" %%a in ("a.txt") do (
   >>"b.txt" echo %%a
   timeout /t 2 /nobreak
)

virad
Posts: 3
Joined: 15 Apr 2014 13:38

Re: Adding a line from one text file to another text file

#3 Post by virad » 15 Apr 2014 21:57

Hey foxdrive,

The system cannot find the file a.txt.

That's what I keep getting. I tried changing the directory and changing the name file. The file is there in the same folder, I don't understand why it can't 'find' it.

virad
Posts: 3
Joined: 15 Apr 2014 13:38

Re: Adding a line from one text file to another text file

#4 Post by virad » 16 Apr 2014 00:00

It worked!!!

Code: Select all

@echo off
for /f "usebackq delims=" %%a in (ab.txt) do (
   >>"b.txt" echo %%a
   timeout /t 2 /nobreak
)


I took out the "" in ("a.txt") so that the file was (a.txt). Also, I renamed my file to 'a' instead of 'a.txt' I don't see the difference but I guess it makes a difference.

I'm guessing as this is running, it is updating the file as in saving the file every time it adds the line. WOW FANTASTIC BABY DANCE.

Thank you so much foxidrive!

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

Re: Adding a line from one text file to another text file

#5 Post by foxidrive » 16 Apr 2014 00:50

virad wrote:

Code: Select all

@echo off
for /f "usebackq delims=" %%a in (ab.txt) do (
   >>"b.txt" echo %%a
   timeout /t 2 /nobreak
)


I took out the "" in ("a.txt") so that the file was (a.txt). Also, I renamed my file to 'a' instead of 'a.txt'


I'm glad you're satisfied. :)

The "" around "a.txt" is supposed to be there as it allows you to use "d:\folder with spaces\filename with spaces.txt" and the term usebackq is what allows this.
Something else interfered in your tests...

Post Reply