Computing.Net > Forums > Programming > Batch file to concatenate files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to concatenate files

Reply to Message Icon

Name: charvett
Date: March 14, 2005 at 11:18:11 Pacific
OS: windows 2000
CPU/Ram: Quad 2Xeon/16GB
Comment:

I'm trying to find a way to concatenate several log files into one file. The files rollup about every hour and I'd like to combine them into a whole day.

Here's an example:

Input files

1. sa_2005.01.27-00.21.00.log
2. sa_2005.01.27-01.21.00.log
3. sa_2005.01.28-00.23.00.log
4. sa_2005.01.28-01.23.00.log

Desired Output files:

1. sa_2005.01.27.log
2. sa_2005.01.28.log

I can use the cygwin utility "cat" to combine the files manually, but I'm struggling to make a batch file that will look at the file name and do the operation automatically.

The manual command for one of the examples above would be:

cat sa_2005.01.27* > 2005.01.27.log

Any help is greatly appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: March 14, 2005 at 22:52:20 Pacific
Reply:

Hi,

Dunno cygwin.

But it could be as simple as:

copy /b file1+file2 file3

You'll have a slight additional challenge with your exact file names because they contain multiple dots.


M2


0

Response Number 2
Name: charvett
Date: March 15, 2005 at 09:34:34 Pacific
Reply:

Ok, so I can use "Copy" as well as "Cat" to do to the same thing. Off Topic - Cygwin produces Unix tools for Windows. "Cat" is a Unix tool for concatenating files.

Back on Topic -

What I'm struggling with is automating the process.

I need to have a batch file that takes the fist eleven characters (13 if you count the dots) and selects those files and concatenates them.

So when it's done the last line in the batch file might look like:

cat %SomeVariable%* > %SomeVariable%.log

I'm not sure I'm describing it clearly. Please let me know if I need to clarify anything.

Thanks!



0

Response Number 3
Name: Mechanix2Go
Date: March 15, 2005 at 10:32:56 Pacific
Reply:

Hi Tom,

This may get you started:

::**
@echo off > 1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.txt

for %%F in (*.txt) do call :eleven %%F

goto :eof

:eleven

set FILEVAR=%1

echo %FILEVAR:~0,13%
::**

This bit:

%FILEVAR:~0,13%

tells it to 'skip none and use the next 13 chars'. [ie use the first 13]

Let us know how it's going.

M2


0

Response Number 4
Name: charvett
Date: March 15, 2005 at 11:29:51 Pacific
Reply:

Thanks for the help, that did pretty much what I wanted it to do with one unexpected behavior.

I used the copy command instead of cat and it works great. The only issue is that it run the copy command for each file over and over again until it gets to the last one. I've got thousands of files.

Last thing I need is for the batch file to see if the file exists before running the copy command.

I'm currently using:

*********************************************
:: @echo off > 1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.txt

for %%F in (*.log) do call :eleven %%F

goto :eof

:eleven

set FILEVAR=%1

copy /b %FILEVAR:~0,13%* %FILEVAR:~0,13%.log
********************************************

So I need some like a "if %FILEVAR:~0,13%.log not exist then" statement.

Does that make sense?

Thanks for the help!



0

Response Number 5
Name: charvett
Date: March 15, 2005 at 11:58:57 Pacific
Reply:

OK, I think I got it:

********************************************
:: @echo off > 1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.txt

for %%F in (*.log) do call :eleven %%F

goto :eof

:eleven

set FILEVAR=%1

if not exist "%FILEVAR:~0,13%.log" copy /b %FILEVAR:~0,13%* %FILEVAR:~0,13%.log
*******************************************

Thanks for all your help!


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: March 15, 2005 at 12:22:03 Pacific
Reply:

Hi Tom,

Yes, it makes sense.

I'll need to massage it some.

[IVO can do this in his sleep]

I think I'm making this harder than it needs to be.

{happens a lot]

Try this:

::**
@echo off > quit.bat

::Input files

echo @ > sa_2005.01.27-00.21.00.log
echo @ > sa_2005.01.27-01.21.00.log
echo @ > sa_2005.01.28-00.23.00.log
echo @ > sa_2005.01.28-01.23.00.log

::Desired Output files:

copy/b sa_2005.01.27*.log sa_2005.01.27.#
copy/b sa_2005.01.28*.log sa_2005.01.28.#

move *.# ..

del ..\*.log
ren ..\*.# *.log
::**

It's using the parent directory to avoid a name collision.

You may want to 'hard wire' the output location to avoid any unpleasant surprises.

HTH

M2


0

Response Number 7
Name: Mechanix2Go
Date: March 15, 2005 at 20:47:42 Pacific
Reply:

Hi Tom,

I think I got most of the knots out of it.

:**
:: If the dates in the names are 0x for 1~9 the %DAY% will need 0 prepended.

@echo off > quit.bat

::*** This section is for testing only. *****
echo 1 > sa_2005.01.27-00.21.00.log
echo 2 > sa_2005.01.27-01.21.00.log
echo 3 > sa_2005.01.28-00.23.00.log
echo 4 > sa_2005.01.28-01.23.00.log

::*** cleanup ***
if exist ..\*.log del ..\*.log
del log.*

::*** run through 12 months ***
for /L %%M in (1,1,12) do call :day %%M
set MON=
set DAY=
goto :eof

::*** run through 31 days ***
:day
::for /L %%D in (3,1,4) do echo %1 %%D >> log.day
for /L %%D in (1,1,31) do call :copier %1 %%D
goto :eof

::*** do the copying ***
:copier
set MON=%1
set DAY=%2
::*** prepend 0 to months 1 to 9 ***
for /L %%S in (1,1,9) do if %MON%==%%S set MON=0%MON%
::*** for testing only ***
echo %MON% %DAY% >> log.cop

if exist sa_2005.%MON%.%DAY%-*.log copy/b sa_2005.%MON%.%DAY%-*.log ..\sa_2005.%MON%.%DAY%.log

:eof
::**


M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to concatenate files

Concatenating files using Batch pro www.computing.net/answers/programming/concatenating-files-using-batch-pro/16679.html

Batch file to count a specific character in a www.computing.net/answers/programming/batch-file-to-count-a-specific-character-in-a/19751.html

Batch file to append created date www.computing.net/answers/programming/batch-file-to-append-created-date/14993.html