Link to home
Start Free TrialLog in
Avatar of zhshqzyc
zhshqzyc

asked on

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.
Avatar of Sommerblink
Sommerblink
Flag of United States of America image

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.

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.
Avatar of Bill Prew
Bill Prew

You can also do it using wildacrds, as in:

copy chr*.tped total.tped


~bp
Avatar of zhshqzyc

ASKER

Can you use a for loop? They are all text files.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Billprew is correct, wildcards are perfectly legal for the source.
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

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
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

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

~bp
Your code is runable, but all the text are displayed on the screen. It is not good.
My files are huge, totally about 46GB.
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
Good. Can you add some code to display the progress such as
 "Please wait,processing chr1.tped"

Open in new window

I guess
 echo processing chr %%A

Open in new window

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
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?
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