Exemple #1
0
 private void sortBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
 {
     sortBackgroundWorker.ReportProgress(1, "文件整理中...");// 进度传出
     for (int i = 0; i < items.Count; i++)
     {
         if (sortBackgroundWorker.CancellationPending)// 取消检测
         {
             e.Cancel = true;
             return;
         }
         sortBackgroundWorker.ReportProgress(Percents.Get(i, items.Count), "正在整理 " + items[i].SubItems[0].Text);// 进度传出
         MoveTo(items[i].SubItems[1].Text, items[i].SubItems[2].Text);
     }
 }
Exemple #2
0
        /// <summary>
        /// 导步整理开始
        /// </summary>
        private void scanBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (items.Count != 0)
            {
                items.Clear();
            }
            scanBackgroundWorker.ReportProgress(1, "文件扫描中...");                 // 进度传出
            DirectoryInfo directorys = new DirectoryInfo(e.Argument as string); // 遍历文件夹

            FileInfo[] files = directorys.GetFiles("*.*", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                if (scanBackgroundWorker.CancellationPending)// 取消检测
                {
                    e.Cancel = true;
                    return;
                }
                scanBackgroundWorker.ReportProgress(Percents.Get(i, files.Length), files[i].FullName);// 进度传出

                ///

                bool rename = Rename(files[i].Name);      // 是否重命名
                bool scan   = !noscanRadioButton.Checked; // 是否整理
                //if (!rename && !scan) continue;//

                #region 关键元素
                string inPath             = files[i].FullName;                                                                                 // 旧路径
                string inName             = Path.GetFileNameWithoutExtension(files[i].Name).Replace("mmexport", "").Replace("wx_camera_", ""); // 不包含扩展名的文件名
                string inExtension        = Path.GetExtension(files[i].Name);                                                                  // 旧扩展名
                string outPath            = "";                                                                                                // 新路径
                string outName            = "";                                                                                                // 不包含扩展名的文件名
                string outExtension       = inExtension.ToLower();                                                                             // 新扩展名
                string specifyFolder      = outFolderLabel.Text;                                                                               // 指定目标文件夹路径
                string originalFolder     = files[i].DirectoryName;                                                                            // 自身文件夹路径
                string originalFolderName = new DirectoryInfo(originalFolder).Name;                                                            // 自身文件夹名字
                string yyyyMMdd           = "";                                                                                                // 年月关键字
                string errers             = "";
                #endregion 关键元素

                ListViewItem listViewItem = new ListViewItem(); // 定义单个项目
                listViewItem.ImageIndex = i;                    //
                //listViewItem.Text = outName+ outExtension;// 项目名
                listViewItem.Text = "待整理";                      // 状态
                listViewItem.SubItems.Add(inPath);              // 文件位置

                try
                {
                    #region 获取年月关键字
                    if (!rename)
                    {
                        yyyyMMdd = files[i].Name.Substring(0, 8);         // 不须重命名时,通过文件名获取年月关键字
                    }
                    else // 需要重命名时,通过读取文件信息获得年月关键字
                    {
                        switch (outExtension) // 扩展名检测
                        {
                        case ".jpg":          // 照片类,必须是相机生成的才有拍摄日期,软件生成的没有
                        case ".jpeg":
                        case ".arw":
                        {
                            yyyyMMdd = DateFunction.GetPhotoData(inPath);        // 获取照片拍摄时间 // 返回空白,即此方法没有获取到有效信息
                            break;
                        }

                        case ".mkv":                                      // Matroska // 多媒体类,MediaInfo支持的格式,读取编码日期
                        case ".mka":                                      // Matroska
                        case ".mks":                                      // Matroska
                        case ".ogg":                                      // Ogg
                        case ".ogm":                                      // Ogg
                        case ".avi":                                      // Riff
                        case ".wav":                                      // Riff
                        case ".mpeg":                                     // Mpeg 1&2
                        case ".mpg":                                      // Mpeg 1&2
                        case ".vob":                                      // Mpeg 1&2
                        case ".mp4":                                      // Mpeg 4 container
                        case ".mpgv":                                     // Mpeg video specific
                        case ".mpv":                                      // Mpeg video specific
                        case ".m1v":                                      // Mpeg video specific
                        case ".m2v":                                      // Mpeg video specific
                        case ".mp2":                                      // Mpeg audio specific
                        case ".mp3":                                      // Mpeg audio specific
                        case ".asf":                                      // Windows Media
                        case ".wma":                                      // Windows Media
                        case ".wmv":                                      // Windows Media
                        case ".qt":                                       // Quicktime
                        case ".mov":                                      // Quicktime
                        case ".rm":                                       // Real
                        case ".rmvb":                                     // Real
                        case ".ra":                                       // Real
                        case ".ifo":                                      // DVD-Video
                        case ".ac3":                                      // AC3
                        case ".dts":                                      // DTS
                        case ".aac":                                      // AAC
                        case ".ape":                                      // Monkey's Audio
                        case ".mac":                                      // Monkey's Audio
                        case ".flac":                                     // Flac
                        case ".dat":                                      // CDXA, like Video-CD
                        case ".aiff":                                     // Apple/SGI
                        case ".aifc":                                     // Apple/SGI
                        case ".au":                                       // Sun/NeXT
                        case ".iff":                                      // Amiga IFF/SVX8/SV16
                        case ".paf":                                      // Ensoniq PARIS
                        case ".sd2":                                      // Sound Designer 2
                        case ".irca":                                     // Berkeley/IRCAM/CARL
                        case ".w64":                                      // SoundFoundry WAVE 64
                        case ".mat":                                      // Matlab
                        case ".pvf":                                      // Portable Voice format
                        case ".xi":                                       // FastTracker2 Extanded
                        case ".sds":                                      // Midi Sample dump Format
                        case ".avr":                                      // Audio Visual Research
                        {
                            yyyyMMdd = DateFunction.GetMediaData(inPath); // 获取媒体拍摄时间 // 返回空白,即此方法没有获取到有效信息
                            break;
                        }

                        default:
                        {
                            yyyyMMdd = "";
                            if (MessageBox.Show("当前文件:" + inPath + "不支持获取拍摄时间\r\n\r\n是否以最后的修改时间整理?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                            {
                                listViewItem.Text = "待整理";        // 状态
                                errers            = "格式不支持获取拍摄时间,以最后的修改时间整理";
                                break;
                            }
                            else
                            {
                                listViewItem.Text = "不整理";        // 状态
                                errers            = "格式不支持获取拍摄时间,不整理";
                                continue;
                            }
                        }
                        }
                        if (yyyyMMdd == null)// 如果遍历完文件信息后,yyyyMM为null,表示通过读取文件信息获得年月关键字,时间格式化成yyyyMM时失败了
                        {
                            DialogResult msg = MessageBox.Show("当前文件:" + inPath + "拍摄时间格式化失败\r\n\r\n点击 “是” 以文件的最后修改时间整理此文件其他文件\r\n\r\n点击 “否” 跳过些文件,继续整理其他文件\r\n\r\n点击 “取消” 停止处理", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                            if (msg == DialogResult.Yes)
                            {
                                listViewItem.Text = "待整理";// 状态
                                errers            = "拍摄时间格式化失败,以最后的修改时间整理";
                                break;
                            }
                            else if (msg == DialogResult.No)
                            {
                                listViewItem.Text = "不整理";// 状态
                                errers            = "拍摄时间格式化失败,不整理";
                                continue;
                            }
                            else
                            {
                                e.Cancel = true;
                                return;
                            }
                        }
                    }
                    if (yyyyMMdd == "")
                    {
                        yyyyMMdd = string.Format("{0:yyyyMMdd}", files[i].LastWriteTime);                // 为空,表示通过读取文件信息获得年月关键字未成功 // 通过文件最后写入时间获得年月关键字
                    }
                    #endregion 获取年月关键字

                    #region 重命名
                    if (rename)// 须重命名时
                    {
                        if (DateFolder(originalFolder))
                        {
                            outName = yyyyMMdd + "_" + originalFolderName + "_" + inName + outExtension;
                        }
                        else
                        {
                            outName = yyyyMMdd + "_" + originalFolderName + "_" + inName + outExtension;
                        }
                        outPath = Path.Combine(originalFolder, outName);
                    }
                    else// 不须重命名时
                    {
                        outName = inName + outExtension;
                        outPath = inPath;
                    }
                    #endregion 重命名

                    #region 整理
                    if (scan)// 不须整理时跳过
                    {
                        //if (yyyyMMdd.Substring(0, 6) == originalFolderName) { }// 自身文件夹名与年月关键字相同时不整理
                        if (specifyRadioButton.Checked)
                        {
                            outPath = Path.Combine(specifyFolder, yyyyMMdd.Substring(0, 6), outName);                            // 整理到指定文件夹
                        }
                        if (originalRadioButton.Checked)
                        {
                            outPath = Path.Combine(originalFolder, yyyyMMdd.Substring(0, 6), outName);                             // 在自身文件夹中整理
                        }
                    }
                    #endregion 获取年月关键字

                    listViewItem.SubItems.Add(outPath); // 文件整理路径
                    listViewItem.SubItems.Add(errers);  // 错误信息
                    items.Add(listViewItem);
                }

                #region 异常
                catch (UnauthorizedAccessException ex)
                {
                    if (MessageBox.Show("无权限操作,请尝试使用管理员权限运行本程序\r\n\r\n当前文件:" + files[i].FullName + "\r\n\r\n错误描述如下\r\n\r\n" + ex + "\r\n\r\n是否继续?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        continue;
                    }
                    else
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                catch (FileNotFoundException ex)
                {
                    if (MessageBox.Show("文件或文件夹不存在\r\n\r\n当前文件:" + files[i].FullName + "\r\n\r\n错误描述如下\r\n\r\n" + ex + "\r\n\r\n是否继续整理?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        continue;
                    }
                    else
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                catch (Exception ex)
                {
                    if (MessageBox.Show("发生未知错误\r\n\r\n当前文件:" + files[i].FullName + "\r\n\r\n错误描述如下" + ex + "\r\n\r\n是否继续整理?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        continue;
                    }
                    else
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                #endregion 异常
            }

            ///
        }