Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Batch file to concatenate files

Original Message
Name: charvett
Date: March 14, 2005 at 11:18:11 Pacific
Subject: Batch file to concatenate files
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!


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: March 14, 2005 at 22:52:20 Pacific
Subject: Batch file to concatenate files
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 2
Name: charvett
Date: March 15, 2005 at 09:34:34 Pacific
Subject: Batch file to concatenate files
Reply: (edit)
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!



Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: March 15, 2005 at 10:32:56 Pacific
Subject: Batch file to concatenate files
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 4
Name: charvett
Date: March 15, 2005 at 11:29:51 Pacific
Subject: Batch file to concatenate files
Reply: (edit)
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!



Report Offensive Follow Up For Removal

Response Number 5
Name: charvett
Date: March 15, 2005 at 11:58:57 Pacific
Subject: Batch file to concatenate files
Reply: (edit)
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!


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: March 15, 2005 at 12:22:03 Pacific
Subject: Batch file to concatenate files
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 7
Name: Mechanix2Go
Date: March 15, 2005 at 20:47:42 Pacific
Subject: Batch file to concatenate files
Reply: (edit)
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


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch file to concatenate files

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC