Introduction
This application does the following:
- 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.
- Each group can be put into a certain folder.
- We can choose which file will be copied, and which one will not be copied.
- 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.
- 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
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;
}
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;
}
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;
}
}
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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.