Click here to Skip to main content
11,578,739 members (61,275 online)
Click here to Skip to main content

Divide Files in Groups by Certain Order

, 1 May 2006 CPOL 18.9K 107 9
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Denny50
Software Developer (Senior)
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

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

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.

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