Batch file to concatenate files

March 14, 2005 at 11:18:11
Specs: windows 2000, Quad 2Xeon/16GB

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!

See More: Batch file to concatenate files

Report •

March 14, 2005 at 22:52:20


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.


Report •

March 15, 2005 at 09:34:34

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.


Report •

March 15, 2005 at 10:32:56

Hi Tom,

This may get you started:

@echo off >

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

goto :eof


set FILEVAR=%1

echo %FILEVAR:~0,13%

This bit:


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

Let us know how it's going.


Report •

Related Solutions

March 15, 2005 at 11:29:51

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 >

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

goto :eof


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!

Report •

March 15, 2005 at 11:58:57

OK, I think I got it:

:: @echo off >

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

goto :eof


set FILEVAR=%1

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

Thanks for all your help!

Report •

March 15, 2005 at 12:22:03

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.



Report •

March 15, 2005 at 20:47:42

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 ***
::for /L %%D in (3,1,4) do echo %1 %%D >>
for /L %%D in (1,1,31) do call :copier %1 %%D
goto :eof

::*** do the copying ***
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



Report •

Ask Question