Click here to Skip to main content
15,356,336 members
Articles / Desktop Programming / MFC
Posted 12 Aug 2002


32 bookmarked

Classes for Splitting and Joining files

Rate me:
Please Sign up or sign in to vote.
4.75/5 (7 votes)
16 Aug 20023 min read
This article describes two classes, one for splitting a file into smaller chunks and another for merging smaller files into one. Also there is a utility written using these classes.


This article explains two classes CFileSplit and CFileMerge.


This class is used for splitting a file into multiple parts. There are two ways to split a file using this :- split into a fixed number of files, or split into files of a specific size each. The class is derived from CWinThread class. Implementing this as a thread means that -

  1. Any application using this class won't freeze while it is processing
  2. You can cancel a lengthy split process. Also the thread will send messages back to the calling window indicating the progress of the split or merge process.

Using CFileSplit is pretty simple. Include the FileSplit.h and FileSplit.cpp files in your project. Create an object of CFileSplit using the AfxBeginThread function and set the input file name, output directory, and how to split ( i.e. fixed no: of files or fixed size files). Also set the m_pMainWnd member of the CWinThread object to the current window. This will enable it to send messages back to it.

CFileSplit *m_pFS;

m_pFS = (CFileSplit*)(AfxBeginThread(RUNTIME_CLASS(CFileSplit)));
m_pFS->m_pMainWnd = this;
m_pFS->m_InputFileName = m_InputFile;
m_pFS->m_OutputDirName = m_OutputDir;
m_pFS->m_nSplitMode    = 0;
m_pFS->m_nSplitVal     = 12;

m_nSplitMode member indicates how to split. 0 means split into a fixed no: of files, in which case the no: of files will be in m_nSplitVal. If m_nSplitMode is 1 it means split into fixed size files, in which case m_nSplitVal will contain the size of each output file in Bytes.

To start the splitting process send a WM_FS_START message to the CFileSplit object. WM_FS_START is an application defined message defined in FileSplit.h


The thread will destroy itself after it is done splitting the files.

If you want to track the progress of a splitting process, the CFileSplit thread will send you an WM_FS_UPDATESTATUS message. The WPARAM of this message will contain the progress in percentage. The LPARAM will be set to 1 when the splitting is complete. You can handle this message in your application; in your message map, add this :-

BEGIN_MESSAGE_MAP(CFileSplitPage, CPropertyPage)

where OnUpdateStatus is the handler function which is declared as:

afx_msg void OnUpdateStatus(WPARAM wP, LPARAM lP);


This class does the opposite of the above; it takes a bunch of files and combines it into one. (copy /b command in DOS can also do the same thing). It is implemented similar to CFileSplit and can be used as follows:

CFileMerge *m_pFM;
m_pFM = (CFileMerge*)AfxBeginThread(RUNTIME_CLASS(CFileMerge));

The m_InFiles member is a CStringArray containing the list of input files to be combined, in the order in which they are to be combined.

m_pFM->m_OutFile = m_OutFile;

You send a WM_FM_START message to start the process. Similar to CFileSpit, you get a WM_FM_UPDATESTATUS message indicating the progress of the merge.

The Sample Application

The sample app provided is written using these above classes. It is a dialog based application but I changed the main Dialog class to derive from CPropertySheet instead of CDialog and added split and merge as two pages to it. (As you can see I was not able to get rid of the 'help' button from the Property sheet, nor add a minimize button).

From the Split Page, you can select the file you want to split, select the output directory and hit the start button. The application creates Files with numbered extensions ( outfile.ext.1, outfile.ext.2 and so on...)

From the merge page select the input files. When you add files, it tries to sort based on the extensions, if they are numbered. If they are not in the right order, you can move them up and down. Hit the start button to start merging. During a long split or merge process, you can hit the cancel button to stop the process.


17 Aug 2002 - updated downloads


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United Kingdom United Kingdom
Has been programming for 7 years, mostly in Visual C++, C# .NET and occasionally in Java

Comments and Discussions

QuestionHow to Restart the CFileSplit Thread Pin
santhoshv8416-Dec-07 18:33
Membersanthoshv8416-Dec-07 18:33 
QuestionHow to Stop and Restart the CFileSplit thread. Pin
santhoshv8416-Dec-07 18:28
Membersanthoshv8416-Dec-07 18:28 
GeneralGood work Pin
v_srinu_26_f25-May-06 4:53
Memberv_srinu_26_f25-May-06 4:53 
GeneralGetting Rid of "Help" And "Apply" Pin
Pepsibot28-May-03 16:46
MemberPepsibot28-May-03 16:46 
GeneralThread in a new dialog Pin
Bartosz Ostrowski8-Sep-02 4:43
MemberBartosz Ostrowski8-Sep-02 4:43 
GeneralRe: Thread in a new dialog Pin
Bijesh10-Sep-02 3:35
MemberBijesh10-Sep-02 3:35 
GeneralRe: Thread in a new dialog Pin
Bartosz Ostrowski11-Sep-02 23:36
MemberBartosz Ostrowski11-Sep-02 23:36 
GeneralAnother way... Pin
Dan Madden14-Aug-02 10:52
MemberDan Madden14-Aug-02 10:52 
GeneralRe: Another way... Pin
Blake Coverett18-Aug-02 11:42
MemberBlake Coverett18-Aug-02 11:42 
GeneralTried it!! It split, but didn't merge. Pin
WREY13-Aug-02 12:23
MemberWREY13-Aug-02 12:23 
GeneralRe: Tried it!! It split, but didn't merge. Pin
Bijesh13-Aug-02 16:10
MemberBijesh13-Aug-02 16:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.