Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Combining Multiple Text Files

3,523 views
Skip to first unread message

Sam

unread,
Oct 25, 2010, 11:05:46 AM10/25/10
to
I get about 50 single text files, that are in the same format, each
day. They all need to be combined in to one text file to be
processed. Is there a way using a batch file to combine all these
files? If so, could someone please provide a sample of the code?

Thanks

Auric__

unread,
Oct 25, 2010, 11:11:08 AM10/25/10
to

for %%x in (*.txt) do type %%x>>templist

(This assumes that a file named "templist" doesn't already exist.)

If there are spaces in any of the text files' names, use this instead:

for %%x in (*.txt) do type "%%x">>templist

--
You wanted to!

Sam

unread,
Oct 25, 2010, 11:25:39 AM10/25/10
to
Ok, so I tried that with a folder with 29 files in it. For some
reason it duplicated records and gave me 59 records in the new file.
Any suggestions?

Auric__

unread,
Oct 25, 2010, 11:52:42 AM10/25/10
to
On Mon, 25 Oct 2010 15:25:39 GMT, Sam wrote:

> On Oct 25, 10:11�am, "Auric__" <not.my.r...@email.address> wrote:
>> On Mon, 25 Oct 2010 15:05:46 GMT, Sam wrote:
>> > I get about 50 single text files, that are in the same format, each
>> > day. �They all need to be combined in to one text file to be
>> > processed. �Is there a way using a batch file to combine all these
>> > files? If so, could someone please provide a sample of the code?
>>
>> for %%x in (*.txt) do type %%x>>templist
>>
>> (This assumes that a file named "templist" doesn't already exist.)
>>
>> If there are spaces in any of the text files' names, use this instead:
>>
>> for %%x in (*.txt) do type "%%x">>templist

> Ok, so I tried that with a folder with 29 files in it. For some
> reason it duplicated records and gave me 59 records in the new file.
> Any suggestions?

(Please don't top-post.)

That's... odd. It works fine for me. Did you replace "templist" with
something that ends in ".txt"? "templist" *should not* end with ".txt" until
*after* the "for" line.

It looks like you're using Windows 7. I have zero experience with anything
newer than XP, and don't really know what's going on. You can try digging
through the FAQ here:
http://www.netikka.net/tsneti/info/tscmd.php

...or you can wait for someone who knows more than me to answer.

--
If swimming is so good for the figure, how do you explain whales?

foxidrive

unread,
Oct 25, 2010, 12:01:24 PM10/25/10
to
On 26/10/2010 02:52, Auric__ wrote:
>>>> I get about 50 single text files, that are in the same format, each
>>>> day. They all need to be combined in to one text file to be
>>>> processed. Is there a way using a batch file to combine all these
>>>> files?

>>> for %%x in (*.txt) do type "%%x">>templist


>
>> Ok, so I tried that with a folder with 29 files in it. For some
>> reason it duplicated records and gave me 59 records in the new file.
>> Any suggestions?
>

> That's... odd. It works fine for me. Did you replace "templist" with
> something that ends in ".txt"? "templist" *should not* end with ".txt" until
> *after* the "for" line.

I suspect that is the issue too.


Another method in NT flavour Windows that should work is

copy *.txt temp.tmp
ren temp.tmp "Complete list.txt"


--
Regards,
Mic

Sam

unread,
Oct 25, 2010, 12:07:43 PM10/25/10
to
I did have the .txt on the "templist" file name --- WORKS GREAT Now!!!
THANKS!!!!!!!!!!!!!!!!!

On Oct 25, 11:01 am, foxidrive <foxidr...@gotcha.woohoo.invalid>
wrote:

Sam

unread,
Oct 25, 2010, 12:13:24 PM10/25/10
to
I have one more question. Is there a way to add the current date to
the end of the "templist" file name. Something like "
"templist_10-22-10"?

> > Mic- Hide quoted text -
>
> - Show quoted text -

foxidrive

unread,
Oct 25, 2010, 12:55:20 PM10/25/10
to
On 26/10/2010 03:13, Sam wrote:
> I have one more question. Is there a way to add the current date to
> the end of the "templist" file name. Something like "
> "templist_10-22-10"?

Yes there is, and it is a frequently asked question.

Timo Salmi posted the FAQ list in the last couple of days -

Subject: Batch file information and FAQs

so have a look there.


--
Regards,
Mic

Sjouke Burry

unread,
Oct 25, 2010, 1:14:46 PM10/25/10
to
copy /a *.txt all.doc
--yourfiles^^
-----collection^^^

foxidrive

unread,
Oct 25, 2010, 1:46:32 PM10/25/10
to

There's an issue with with either of these commands
when the last line of the text file doesn't end with a carriage return.

@echo off
copy *.txt file.doc


@echo off
for %%a in (*.txt) do type %%a >>file.doc

You can end up with this in the file

===[paste]===
Last line of file oneFirst line of file two
===[paste]===

I found out that when using MORE it eliminates this issue

@echo off
for %%a in (*.txt) do more %%a >>file.doc


and then you'll get this if the last line has a trailing CR or not.

===[paste]===
Last line of file one
First line of file two
===[paste]===


Timo Salmi

unread,
Oct 26, 2010, 9:40:56 AM10/26/10
to
On 25.10.2010 20:46 foxidrive wrote:
> ===[paste]===
> Last line of file oneFirst line of file two
> ===[paste]===
>
> I found out that when using MORE it eliminates this issue
>
> @echo off
> for %%a in (*.txt) do more %%a >>file.doc

Thanks. I adapted the example to elaborate

How do I test and ensure that <CR><LF> is the last character pair of
my log file?
http://www.netikka.net/tsneti/info/tscmd163.htm

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

Sam

unread,
Oct 26, 2010, 4:08:02 PM10/26/10
to
One more issue I''ve found. My files have a header row that I dont
need. Is there anything I could do to start at Row 2 of each of the
files. If so what would the code be?

Thanks for the Help!

On Oct 26, 8:40 am, Timo Salmi <t...@uwasa.fi> wrote:
> On 25.10.2010 20:46 foxidrive wrote:
>
> > ===[paste]===
> > Last line of file oneFirst line of file two
> > ===[paste]===
>
> > I found out that when using MORE it eliminates this issue
>
> > @echo off
> > for %%a in (*.txt) do more %%a >>file.doc
>
> Thanks. I adapted the example to elaborate
>
>   How do I test and ensure that <CR><LF> is the last character pair of
> my log file?
>  http://www.netikka.net/tsneti/info/tscmd163.htm
>
>     All the best, Timo
>
> --

> Prof. Timo Salmi   mailto:t...@uwasa.fi    ftp &http://garbo.uwasa.fi/

Sjouke Burry

unread,
Oct 26, 2010, 9:53:45 PM10/26/10
to
Sam wrote:
> One more issue I''ve found. My files have a header row that I dont
> need. Is there anything I could do to start at Row 2 of each of the
> files. If so what would the code be?
>
Google for unixtools.
Tail.exe has the option to print everything
from line n to the end

So tail +2 ape.txt prints from line 2
Append that output to your output file and thats it.

something like:
del *.tmp
for %p in (*.txt) do tail +2 %p >>myoutput.tmp
ren myoutput.tmp myoutput.txt

foxidrive

unread,
Oct 27, 2010, 3:37:51 AM10/27/10
to

In XP+ versions of Windows the more command has a switch to start at an
offset too.


@echo off
for %%a in (*.txt) do more +1 %%a >>file.doc

--
Regards,
Mic

FileGod

unread,
Oct 29, 2010, 1:32:24 PM10/29/10
to
Sam <samc...@gmail.com> wrote:
>One more issue I''ve found. My files have a header row that I dont
>need. Is there anything I could do to start at Row 2 of each of the
>files. If so what would the code be?

This will remove the string (Your Header) from a text file...
findstr /v "Your Header" Old.txt>New.txt

http://www.filegod.netfirms.com

Chauffeurtje

unread,
Nov 12, 2010, 7:14:44 PM11/12/10
to

"Sam" <samc...@gmail.com> schreef in bericht
news:7e4438e3-3606-474d...@a37g2000yqi.googlegroups.com...

This is not diicult at all. Put all the txt files into 1 directory. Than do
this Copy *.txt :c\> D (whereas D is a directory or file you DON'T have
under C:\> whereas D is just an example)

Now, use your wordprocessor, and open up D..... and you'll find all the
files you had copied as 1 file. They merged into 1 file. Use the
wordprocessor to place them into the right sequence. Ready, done.

Good luck.

Cor

arunava...@gmail.com

unread,
Jul 15, 2013, 11:38:57 AM7/15/13
to

foxidrive

unread,
Jul 15, 2013, 6:31:56 PM7/15/13
to
Did you notice that the request was written 3 years ago? :)

--
foxi

Todd Vargo

unread,
Jul 15, 2013, 8:51:15 PM7/15/13
to
Google has this powerful, searchable, database of archived newsgroup
postings but can not seem to get it to block responses to threads over 3
months old. Shame on Google.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
0 new messages