• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

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.
0
zhshqzyc
Asked:
zhshqzyc
  • 7
  • 7
  • 3
1 Solution
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now