Ejemplo n.º 1
0
        public static void AddFolderInfo(MyFolderInfo folderInfo, string sourceFile, string destinationFile)
        {
            string srcFolderPathName = System.IO.Path.GetDirectoryName(sourceFile);
            string tarFolderPathName = System.IO.Path.GetDirectoryName(destinationFile);

            folderInfo.CopyIfNotExist(srcFolderPathName, tarFolderPathName);

            //1. get src folder name.
            string srcInfoText = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(sourceFile));//srcFolderName

            if (!folderInfo.isJustRecorded(srcInfoText, tarFolderPathName))
            {
                //2. append to "moreMediaInfo.txt" under tar folder.
                folderInfo.Record(srcInfoText, tarFolderPathName);
            }
        }
Ejemplo n.º 2
0
        // 1. Search file with filename extension list
        // 2. Rebuild target file by format and exif data
        // 3. Search target location, create folder if not exist (Reference source folder name)
        // 4. Check file duplication, then ...?
        //        duplication rule: same name with size and time, may binary.
        //        move to duplication folder, add dup number with dup the dup files.
        // 5. Move /Copy file to target location
        // 6. [Move] also check and remove the empty folder. (Remove Thumb.db picasa.ini)
        public void Run(RunOption sc)
        {
            if (project == null)
            {
                return;
            }
            //project = new UserProject();
            int          nFileCheckCount = 0, nFileActCount = 0;
            MyFolderInfo folderInfo = new MyFolderInfo();

            StreamWriter swDamaged = new StreamWriter(sc.Report_DamagedFilePathame);

            swDamaged.WriteLine("[Start at " + DateTime.Now + "]");
            StreamWriter swIgnored = new StreamWriter(sc.Report_IgnoredFilePathame);

            swIgnored.WriteLine("[Start at " + DateTime.Now + "]");
            swDamaged.AutoFlush = true;
            swIgnored.AutoFlush = true;

            StreamWriter swCopyFailed = null;

            if (project.isContentConfirmByComparedBinary && (project.fAction == UserProject.FileAction.Copy || project.fAction == UserProject.FileAction.CopyThenDelete))
            {
                swCopyFailed = new StreamWriter(sc.Report_CopyFailedFilePathame);
                swCopyFailed.WriteLine("[Start at " + DateTime.Now + "]");
                swCopyFailed.AutoFlush = true;
            }

            ForeachFolderFile((fi) => {
                //ProgressUpdate.Invoke(this, fi.FullName);
                nFileCheckCount++;
                string sourceFile      = fi.FullName;
                string destinationFile = string.Empty, destinationDupFile = string.Empty, FileState = string.Empty;
                string CameraModel     = string.Empty;
                DateTime fileDateTime  = new DateTime();

                //support file Extensions
                //if (Array.IndexOf(project.imageFileExts, fi.Extension.ToLower()) >= 0)
                if (project.imageFileExts.Contains(fi.Extension.ToLowerInvariant()))
                {
                    fileDateTime = fi.LastWriteTime;

                    if (EXIF.FileExtensions.Contains(fi.Extension.ToLowerInvariant()))
                    {
                        EXIF exif = Util.GetExif(fi.FullName);
                        if (exif == null)
                        {
                            fileDateTime = new DateTime(); //Skip act with this file.
                            FileState    = "檔案損毀";
                            swDamaged.WriteLine(fi.FullName);
                            sc.Report_DamagedFileCount++;
                        }
                        else
                        {
                            CameraModel = exif.EquipmentModel;
                            DateTime?dt = exif.ExifDTOriginal;
                            if (dt != null)
                            {
                                fileDateTime = (DateTime)dt;
                            }

                            FileState = fileDateTime.ToString();
                        }
                    }
                }
                else if (project.videoFileExts.Contains(fi.Extension.ToLowerInvariant()))
                //if (Array.IndexOf(project.videoFileExts, fi.Extension.ToLower()) >= 0)
                {
                    fileDateTime = fi.LastWriteTime;
                    //Console.WriteLine(">" + fi.FullName + fi.LastWriteTime.ToLocalTime().ToString());
                    //} else if (fi.Name == "Info")
                    //{
                    //    //Merge to target .. But we don't know the target path!
                }
                else
                {
                    FileState = "忽略";
                    swIgnored.WriteLine(fi.FullName);
                    sc.Report_IgnoredFileCount++;
                }

                if (!fileDateTime.Equals(new DateTime()))
                {
                    //ProgressUpdate.Invoke(this, fi.FullName);
                    //Destination file folder and file name.
                    destinationFile        = BuildDestinationFullFilePathname(fi, fileDateTime, project);
                    string destinationPath = System.IO.Path.GetDirectoryName(destinationFile);

                    //File Action
                    switch (project.fAction)
                    {
                    case UserProject.FileAction.CopyThenDelete:
                    case UserProject.FileAction.Copy:
                        bool isCopySuccess = false;
                        if (!Util.ifHasTheSameFile(sourceFile, destinationFile, project.isTheSameContentByComparedBinary))
                        {
                            if (!Directory.Exists(destinationPath))
                            {
                                Directory.CreateDirectory(destinationPath);    //create if folder not exist
                            }

                            System.IO.File.Copy(sourceFile, destinationFile);
                            Util.GetHash_WithGenToHashInfoFile(destinationFile);
                            isCopySuccess = true;
                        }

                        AddFolderInfo(folderInfo, sourceFile, destinationFile);

                        if (isCopySuccess)
                        {
                            if (project.isContentConfirmByComparedBinary)
                            {
                                if (!Util.ifHasTheSameFile(sourceFile, destinationFile, true))
                                {
                                    //Result is not the same!
                                    isCopySuccess = false;
                                    if (swCopyFailed != null)
                                    {
                                        swCopyFailed.WriteLine(sourceFile);
                                        sc.Report_CopyFailedFileCount++;
                                    }
                                }
                            }
                            if (project.fAction == UserProject.FileAction.CopyThenDelete && isCopySuccess)
                            {
                                System.IO.File.Delete(sourceFile);
                            }
                        }
                        break;

                    case UserProject.FileAction.Move:
                        if (Util.ifHasTheSameFile(sourceFile, destinationFile, project.isTheSameContentByComparedBinary))
                        {
                            //get Destination Duplicated foder name path
                            string phase2   = destinationFile.Substring(project.targetRootPathname.Length);
                            destinationFile = project.targetRootPathname + @"\Dup" + phase2;
                            destinationPath = System.IO.Path.GetDirectoryName(destinationFile);    //Update new path
                        }
                        if (!Directory.Exists(destinationPath))
                        {
                            Directory.CreateDirectory(destinationPath);    //create if folder not exist
                        }

                        MoveWithoutReplace(sourceFile, destinationFile, (CameraModel == string.Empty) ? "INCREASENUMBER" : CameraModel);
                        AddFolderInfo(folderInfo, sourceFile, destinationFile);
                        break;

                    case UserProject.FileAction.ListOnly:
                    default:
                        break;
                    }
                    nFileActCount++;
                    FileState = destinationFile;
                }
                ProgressUpdate.Invoke(this, sourceFile + PREDEF._ACTIONSIGNSTRING_ + FileState);

                //Leave progress controlable.
                bool ret = true;
                switch (sc.Step_ForceProgressTo)
                {
                case "pause":
                    do
                    {
                        Thread.Sleep(1000);
                    } while (sc.Step_ForceProgressTo == "pause");
                    break;

                case "resume":
                    break;

                case "stop":
                    ret = false;
                    break;
                }
                return(ret);
            });

            string FileEndStr = "[End at " + DateTime.Now + "]";

            swDamaged.WriteLine(FileEndStr);
            swDamaged.Close();
            swDamaged.Dispose();
            swIgnored.WriteLine(FileEndStr);
            swIgnored.Close();
            swIgnored.Dispose();
            if (swCopyFailed != null)
            {
                swCopyFailed.WriteLine(FileEndStr);
                swCopyFailed.Close();
                swCopyFailed.Dispose();
            }

            ProgressUpdate.Invoke(this, string.Format(@"Total Files: {0}, Total Act Filecount: {1}",
                                                      nFileCheckCount,
                                                      nFileActCount));
        }