Example #1
0
 public void ReadData(string fileName)
 {
     searchData = new SearchData();
     searchData.ReadXml(fileName);
 }
Example #2
0
        /// <summary>
        /// _searchDuplicate - creates new thread and get the data
        /// </summary>
        /// <param name="startPath"></param>
        /// <param name="extension"></param>
        /// <param name="includeTempFiles"></param>
        /// <returns></returns>
        SearchData _searchDuplicate(string startPath, string extension, bool includeTempFiles, Logger log)
        {
            if (QuitNow)
            {
                return(searchData);
            }
            SendMessage("FileSearcher Starting in " + startPath);

            IEnumerable <string> directories = new List <string>();

            //Enumerate all child directories
            if (Directory.Exists(startPath))
            {
                directories = Directory.EnumerateDirectories(startPath);
                if (directories != null && directories.Count <string>() >= 0)
                {
                    foreach (string directory in directories)
                    {
                        DirectoryInfo di = new DirectoryInfo(directory);
                        if (((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) ||
                            ((di.Attributes & FileAttributes.System) == FileAttributes.System) ||
                            ((di.Attributes & FileAttributes.Temporary) == FileAttributes.Temporary) && includeTempFiles)
                        {
                            continue;
                        }
                        //IAsyncResult ar = BeginSearcherAsync(directory, extension, includeTempFiles, log, CallbackMethod);
                        SearchData dataInDir = _searchDuplicate(directory, extension, includeTempFiles, log);
                        if (dataInDir != null)
                        {
                            searchData.Merge(dataInDir);
                        }

                        if (QuitNow)
                        {
                            return(searchData);
                        }
                    }
                }
            }

            //Enumerate files in that directory only.
            IEnumerable <string> files = Directory.EnumerateFiles(startPath, extension, SearchOption.TopDirectoryOnly);

            //string[] files = Directory.GetFiles(startPath, extension, SearchOption.AllDirectories);
            SendMessage("Files in " + startPath + " Received.");
            SendMessage(" Processing files in " + startPath);
            foreach (string file in files)
            {
                //Quit with available result if Cancellation requested
                if (QuitNow)
                {
                    return(searchData);
                }
                FileInfo fi = new FileInfo(file);

                if (((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) ||
                    ((fi.Attributes & FileAttributes.System) == FileAttributes.System) ||
                    ((fi.Attributes & FileAttributes.Temporary) == FileAttributes.Temporary) && includeTempFiles)
                {
                    continue;
                }

                //To compare we dont need to use file name.
                //1 # Possible duplicate - Compare = file length and extension and last update time.
                //2 # Matching different version - Compare = file name and file length and extension
                //3 # Exact match file name and file length and extension and last update time
                string rowID = fi.Name + fi.Length.ToString() + fi.Extension;

                //Search for an exact duplicate
                if (searchData.Documents.FindByID(rowID + fi.LastWriteTimeUtc.Ticks.ToString()) != null)  //log a duplicate file found
                {
                    SearchData.ExactDuplicateRow counterRow = searchData.ExactDuplicate.NewExactDuplicateRow();
                    counterRow.FileName         = fi.Name;
                    counterRow.ID               = rowID;
                    counterRow.ID_NoVersion     = rowID;
                    counterRow.FilePath         = fi.DirectoryName;
                    counterRow.LastModifiedTime = fi.LastWriteTimeUtc;
                    counterRow.Size             = fi.Length / 1024;
                    if (searchData.ExactDuplicate.FindByIDFilePath(counterRow.ID, counterRow.FilePath) == null)
                    {
                        searchData.ExactDuplicate.AddExactDuplicateRow(counterRow);
                    }
                }

                //Search for another version(Not an exact duplicate version)
                else if (searchData.Documents.FindByID(rowID) != null &&
                         searchData.Documents.Any(r => r.ID.StartsWith(rowID)))
                {
                    SearchData.MultipleVersionRow multipleVersionRow = searchData.MultipleVersion.NewMultipleVersionRow();
                    multipleVersionRow.FileName         = fi.Name;
                    multipleVersionRow.ID_NoVersion     = rowID;
                    multipleVersionRow.ID               = rowID + fi.LastWriteTimeUtc.Ticks.ToString();
                    multipleVersionRow.FilePath         = fi.DirectoryName;
                    multipleVersionRow.LastModifiedTime = fi.LastWriteTimeUtc;
                    multipleVersionRow.Size             = fi.Length / 1024;
                    searchData.MultipleVersion.AddMultipleVersionRow(multipleVersionRow);
                }
                else// This file is not yet in the list
                {
                    SearchData.DocumentsRow row = searchData.Documents.NewDocumentsRow();
                    row.LastModifiedTime = fi.LastWriteTimeUtc;
                    row.ID_NoVersion     = rowID;
                    row.ID       = rowID + fi.LastWriteTimeUtc.Ticks.ToString();
                    row.FileName = fi.Name;
                    row.Size     = fi.Length / 1024;
                    row.FilePath = fi.DirectoryName;
                    searchData.Documents.AddDocumentsRow(row);
                }


                searchData.AcceptChanges();
            }


            SendMessage("processing complete " + startPath + "... now saving");

            return(searchData);
        }