Combine files by Windows Batch File

Hello, I have 22 files chr1.tped, chr2.ped,... chr22.ped.
I want to merge(append) them together to a big file total.tped. Need help for a batch file.

Many thanks.
zhshqzycAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SommerblinkCommented:
The command 'copy' will concatenate files.

Example:
File1.dat
File2.dat
File3.dat into TotalFile.Dat

copy /b file1.dat+file2.dat+file3.dat TotalFile.Dat

The /b is if it is a binary file.
Substute with /a if it is ASCII.

0
SommerblinkCommented:
Forgot one thing at the end:

Should read "copy /b file1.dat+file2.dat+file3.dat TotalFile.Dat /b"

But honestly, I've gone without the '/b's before and things have worked out.
0
Bill PrewCommented:
You can also do it using wildacrds, as in:

copy chr*.tped total.tped


~bp
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

zhshqzycAuthor Commented:
Can you use a for loop? They are all text files.
0
Bill PrewCommented:
No need for a FOR, see my post.

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SommerblinkCommented:
Billprew is correct, wildcards are perfectly legal for the source.
0
zhshqzycAuthor Commented:
But I found the copy order is
chr1.tped
chr10.tped
chr11.tped
...
chr2.tped
chr20.tped
chr21.tped
chr22.tped

Open in new window

What I want is
chr1.tped
chr2.tped
chr3.tped
...
chr9.tped
chr10.tped
..

Open in new window

0
Bill PrewCommented:
I suspect I know the answer, but I have to ask it.  Is there any possibility to create the files with a leading 0, so rather than chr1.tped make that chr01.tped?

If not, then are there any missing files?  Would it be okay to specify the last file name?  If so, then you could do this:

@echo off
set BaseDir=c:\temp
if exist "%BaseDir%\total.tped" del "%BaseDir%\total.tped"
for /L %%A in (1,1,50) do type "%BaseDir%\chr%%A.tped" "%BaseDir%\total.tped"

Open in new window

This will copy files chr1.tped thru chr50.tped to total.tped in the correct order.

~bp
0
zhshqzycAuthor Commented:
You are right. There is no a leading 0.
Just
 1,2,3,4,5,6,7,8,910,11,12,13,14,15,16,17,18,19,20,21,22

Open in new window

0
Bill PrewCommented:
Okay, give that lasp post of mine a shot.  Or let me know if you have any questions.

~bp
0
zhshqzycAuthor Commented:
Your code is runable, but all the text are displayed on the screen. It is not good.
My files are huge, totally about 46GB.
0
Bill PrewCommented:
Sorry about that, one small bug, try this.

@echo off
set BaseDir=c:\temp
if exist "%BaseDir%\total.tped" del "%BaseDir%\total.tped"
for /L %%A in (1,1,50) do type "%BaseDir%\chr%%A.tped" >>"%BaseDir%\total.tped"

Open in new window

~bp
0
zhshqzycAuthor Commented:
Good. Can you add some code to display the progress such as
 "Please wait,processing chr1.tped"

Open in new window

0
zhshqzycAuthor Commented:
I guess
 echo processing chr %%A

Open in new window

0
Bill PrewCommented:
Sure, how about this:

@echo off
set BaseDir=c:\temp
if exist "%BaseDir%\total.tped" del "%BaseDir%\total.tped"
for /L %%A in (1,1,50) do (
  echo Processing: "%BaseDir%\chr%%A.tped"
  type "%BaseDir%\chr%%A.tped" >>"%BaseDir%\total.tped"
)

Open in new window

~bp
0
zhshqzycAuthor Commented:
Thank you very much! It works.
I do have some files which contain leading 0s.
The format
01_jas.bim
02_jas.bim
...
09_jas.bim
10_jas.bim
11_jas.bim
12_jas.bim
....
22_jas.bim

Open in new window

Then how to?
0
Bill PrewCommented:
This could be shortened up a small amount, but since the script is fairly small anyway I just took the obvious approach.  Let me know if this works.  It will first look for the file without a leading 0, then if not found with a leading 0.  If neither file exists nothing is added to the output file.

@echo off

REM Define folder to process files in
set BaseDir=c:\temp

REM Delete the merged result file if it already exists
if exist "%BaseDir%\total.tped" del "%BaseDir%\total.tped"

REM Try to merge up to 50 files together, in correct order
for /L %%A in (1,1,50) do (
  REM See if the file name exists without a leading zero
  if exist "%BaseDir%\chr%%A.tped" (
    echo Processing: "%BaseDir%\chr%%A.tped"
    type "%BaseDir%\chr%%A.tped" >>"%BaseDir%\total.tped"
  ) else (
    REM See if the file name exists with a leading zero
    if exist "%BaseDir%\chr0%%A.tped" (
      echo Processing: "%BaseDir%\chr0%%A.tped"
      type "%BaseDir%\chr0%%A.tped" >>"%BaseDir%\total.tped"
    )
  )
)

Open in new window

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.