/** * Recursively traverse a directory tree converting files * */ private void Convert(string parentDir, string[] fileNames, BackgroundWorker worker, DoWorkEventArgs e) { // Abort the operation if the user has canceled. // Note that a call to CancelAsync may have set // CancellationPending to true just after the // last invocation of this method exits, so this // code will not have the opportunity to set the // DoWorkEventArgs.Cancel flag to true. This means // that RunWorkerCompletedEventArgs.Cancelled will // not be set to true in your RunWorkerCompleted // event handler. This is a race condition. if (worker.CancellationPending) { e.Cancel = true; } else { DirectoryInfo outputDir = new DirectoryInfo(Path.Combine(baseDir + "_Unicode", parentDir.Substring(baseDir.Length))); outputDir.Create(); foreach (string name in fileNames) { if (File.Exists(name)) { // Process files FileInfo file = new FileInfo(name); string fileExtension = file.Extension.ToLower(); if (Regex.IsMatch(fileExtension, patternOfBinaryExt)) { continue; // skip binary files } if (cheAll) { string[] arrsourceEncoding = new string[] { }; for (int i = 0; i < arrsourceEncoding.Length; i++) { IDocConverter docConverter = DocConverterFactory.CreateConverter(docConverters, fileExtension, sourceEncoding); docConverter.Convert(outputDir, file); } } else { IDocConverter docConverter = DocConverterFactory.CreateConverter(docConverters, fileExtension, sourceEncoding); docConverter.Convert(outputDir, file); } worker.ReportProgress(100, file.FullName); } else if (Directory.Exists(name)) { // Process subdirectories Convert(name, Directory.GetFiles(name, getFilter(filterIndex)), worker, e); // files Convert(name, Directory.GetDirectories(name), worker, e); // subdirectories } } } }
public void ProcessFile(string fileNameAndPath, string outputDir, string failedDir, string processedDir, string callerRef, string sentTo) { DocConverterFactory factory = new DocConverterFactory(); factory._logFile = _logFile; factory.StartConverter(); FileInfo file = new FileInfo(fileNameAndPath); try { IDocConverter converter = factory.GetDocConverter(file.FullName); converter.Convert(file.FullName, file.FullName, outputDir, processedDir, callerRef, sentTo); } catch (Exception e) { string mailMsg = "The file ''" + file.FullName + "'' has been moved to the directory " + failedDir + @"\" + Properties.Settings.Default.FailedSubDirConvert + ";" + Environment.NewLine + Environment.NewLine + "Reason:" + Environment.NewLine + "There was an issue converting this file to TIF format. " + Environment.NewLine + e.Message; MailUtils.SendEmail("Error", "Converting File", mailMsg, fileNameAndPath); FileUtils.MoveToDir(file, failedDir + @"\" + Properties.Settings.Default.FailedSubDirConvert); throw new FormatConversionException("Error: ''" + file.FullName + "'' has been moved to the directory " + failedDir + "; " + Environment.NewLine + e.Message); } factory.ShutdownConverter(); }