Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Article

Divide Files in Groups by Certain Order

Rate me:
Please Sign up or sign in to vote.
3.50/5 (7 votes)
1 May 2006CPOL1 min read 28.2K   149   9   1
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.

    C#
    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:

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

    C#
    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:

    C#
    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:

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

    Then, we can group files by:

    C#
    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:

    C#
    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)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralArticle Format Pin
Trance Junkie1-May-06 21:59
Trance Junkie1-May-06 21:59 

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.