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.
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:
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 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
:** :: 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
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