Click here to Skip to main content
Licence CPOL
First Posted 1 May 2006
Views 15,539
Downloads 47
Bookmarked 9 times

Divide Files in Groups by Certain Order

By | 1 May 2006 | Article
This application divides files into groups
Sample Image - Divide_Files_in_Groups.jpg

Introduction

This application does the following:

  1. Divides files less than a certain size into groups. For example, if we want to burn files into CDR, we may divide files into groups less than 670 MB.
  2. Each group can be put into a certain folder.
  3. We can choose which file will be copied, and which one will not be copied.
  4. We can copy files in a certain order. For example, if the files are the video of a surveillance camera, we need to copy files by the order of date. Otherwise we may need to search many disks to find the video of an hour.
  5. Files may be in many sessions. For example, we don't want the video of an hour to be divided into 2 groups.

Application Comments

  1. Read file names from disk. We use a recursion function to do so.

    public int BuildFiles(string RootPath)
    {
        foreach (string fi in Directory.GetFiles(RootPath)) 
        {
            FileInfo fiFile = new FileInfo(fi);
            if(ValidFile(fiFile) == false)
            {
                continue;
            }
            _fileList.Add(fiFile);
            m_nTotalFiles++;
            m_nTotalSize = m_nTotalSize + fiFile.Length; 
            if(fiFile.Length > m_nMaxFileSize)
            {
                m_nMaxFileSize = fiFile.Length;
            }
        }
        if(m_bIncludeSubdirectries)
        {
            foreach (string fi in Directory.GetDirectories(RootPath)) 
            {
                BuildFiles(fi);
            }
        }
    
        return 0;
    }

    Function ValidFile tells us which file would to be copied. If we copy all files, it could be:

    private bool ValidFile(FileInfo InputFile)
    {
        return true;
    }
  2. Sort files. Firstly we choose a compare function:

    public class FileNameComparer : IComparer  
    {
        int IComparer.Compare( Object x, Object y )  
        {
            return ((FileInfo)x).Name.CompareTo(((FileInfo)y).Name);
        }
    }

    Then use this function to sort files:

    public int SortFiles()
    {
        FileNameComparer fcomp = new FileNameComparer();
        _fileList.Sort(fcomp);
        return 0;
    }
  3. Divide the files into groups. First we write a function to decide how to recognize a session. For example, if the file name contains dates, and we don't want files of the same day in different groups, we can use:

    private int FileInGroup(string Filename1, string Filename2)
    {
        return string.Compare(Filename1, 0, Filename2, 0, 8, false);
    }

    Then, we can group files by:

    public int GroupFiles(int GroupSize)
    {
        long nGroupSize = GroupSize * SIZE_OF_MB;
        long nNowSize = 0, nSessionSize = 0;
        int nCount = 0, nNumOfSession = 0;
        string sLastFile = "";
        _groupList.Clear();
    
        try
        {
            if(nGroupSize < m_nMaxFileSize) return -1;
        
            foreach (FileInfo fi in _fileList)
            {
                if(FileInGroup(fi.Name, sLastFile) == 0)
                {
                    nNumOfSession = nNumOfSession + 1;
                    nSessionSize = nSessionSize + fi.Length;
                    if(nSessionSize > nGroupSize)
                    {
                        return -2;
                    }
                }
                else
                {
                    if(nNowSize + nSessionSize > nGroupSize)
                    {
                        if(nCount > 0)
                        {
                            _groupList.Add(nCount);
                        }
                        nCount = nNumOfSession;
                        nNowSize = nSessionSize;
                    }
                    else
                    {
                        nCount = nCount + nNumOfSession;
                        nNowSize = nNowSize + nSessionSize;
                    }
                    nNumOfSession = 1;
                    nSessionSize = fi.Length;
                }
                sLastFile = fi.Name;
            }
        
            if(nCount + nNumOfSession > 0) 
            {
                if(nNowSize + nSessionSize > nGroupSize)
                {
                    if(nCount > 0)
                    {
                        _groupList.Add(nCount);
                    }
                    if(nNumOfSession > 0)
                    {
                        _groupList.Add(nNumOfSession);
                    }
                }
                else
                {
                    _groupList.Add(nCount + nNumOfSession);
                }
            }
            return 0;
        }
        catch(Exception e)
        {
            Debug.WriteLine("Error GroupFiles - Message: " + e.Message );
            return -100;
        }
    }
  4. The last thing is to copy files. We create a new thread to copy files:

    ThreadStart mThreadDelegate = new ThreadStart(Copier.CopyFiles);
    Thread mCopyThread = new Thread(mThreadDelegate);
    mCopyThread.Start();

History

  • 1st May, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Denny50

Software Developer (Senior)

Australia Australia

Member

Senior software engineer with 10 years experience of C/C++ programming. Now also has some project with C#. I'm from Shanghai and now working at Melbourne. At I spare time, I enjoy my family life. My blog, Childhood is a collection about my family, epically my lovely son. Most of them write in Chinese, and a few in my poor English - please forgive me, I'm improving my English as soon as I can.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralArticle Format PinmemberTrance Junkie21:59 1 May '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 2 May 2006
Article Copyright 2006 by Denny50
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid