static string get_file_abs_path(string file, bool add_time_mark, bool append_output) { string file_abs_path = file; if (!file_abs_path.Contains(":")) { file_abs_path = FileSystemRoutines.CreateDirectory((Session.This != null ? Session.This.Dir : Log.RootDir) + "\\" + OUTPUT_DIR_NAME) + "\\" + file_abs_path; } if (add_time_mark) { string perfix = "_" + DateTime.Now.ToString("yyMMddHHmmss"); int point = file_abs_path.LastIndexOf("."); if (point < 0) { file_abs_path = file_abs_path + perfix; } else { file_abs_path = file_abs_path.Insert(point, perfix); } } if (!append_output) { string fap2 = file_abs_path; int counter = 0; while (File.Exists(fap2)) { fap2 = Regex.Replace(fap2, "(" + Regex.Escape(Path.GetFileNameWithoutExtension(file_abs_path)) + @"_)(\d+)", (Match m) => { counter = int.Parse(m.Groups[2].Value) + 1; return(m.Groups[1].Value + counter); } ); if (counter < 1) { fap2 = Regex.Replace(fap2, @"\.[^\.]+$", @"_1$0"); } } file_abs_path = fap2; } return(file_abs_path); }
static public void Run(Action <int, int> progress) { Log.Main.Inform("STARTED"); progress(0, 0); if (string.IsNullOrWhiteSpace(Settings.General.InputFolder)) { Win.LogMessage.Error("Input Folder is not specified."); return; } if (!Directory.Exists(Settings.General.InputFolder)) { Win.LogMessage.Error("Input folder '" + Settings.General.InputFolder + "' does not exist."); return; } string output_records_file = FileSystemRoutines.CreateDirectory(Settings.General.OutputFolder) + "\\_output.csv"; if (File.Exists(output_records_file)) { File.Delete(output_records_file); } TextWriter tw = new StreamWriter(output_records_file, false); List <Template2> active_templates = Settings.Template2s.Template2s.Where(x => x.Active).OrderBy(x => x.OrderWeight).ThenByDescending(x => { return(Settings.TemplateLocalInfo.GetInfo(x.Template.Name).UsedTime); }).ToList(); if (active_templates.Count < 1) { Win.LogMessage.Error("There is no active template!"); return; } List <string> headers = new List <string> { "File", "Template", "First Page", "Last Page", "Invoice", "Total", "Product Name", "Product Cost" }; tw.WriteLine(FieldPreparation.GetCsvHeaderLine(headers, FieldPreparation.FieldSeparator.COMMA)); List <string> files = FileSystemRoutines.GetFiles(Settings.General.InputFolder, false); files.Remove(output_records_file); files = files.Select(f => new FileInfo(f)).Where(fi => !fi.Attributes.HasFlag(FileAttributes.Hidden)).Select(fi => fi.FullName).ToList(); int processed_files = 0; int total_files = files.Count; List <string> failed_files = new List <string>(); progress(processed_files, total_files); foreach (string f in files) { try { bool?result = PdfProcessor.Process(f, active_templates, (templateName, firstPageI, lastPageI, document) => { { List <string> values = new List <string>() { PathRoutines.GetFileName(f), templateName, firstPageI.ToString(), lastPageI.ToString(), document.Invoice, document.Total, "", "" }; tw.WriteLine(FieldPreparation.GetCsvLine(values, FieldPreparation.FieldSeparator.COMMA)); } foreach (PdfProcessor.Document.Product p in document.Products) { List <string> values = new List <string>() { "", "", "", "", "", "", p.Name, p.Cost }; tw.WriteLine(FieldPreparation.GetCsvLine(values, FieldPreparation.FieldSeparator.COMMA)); } }); if (result != true) { failed_files.Add(f); } } catch (Exception e) { Log.Main.Error("Processing file '" + f + "'", e); failed_files.Add(f); } progress(++processed_files, total_files); } tw.Close(); try { System.Diagnostics.Process.Start(output_records_file); } catch { } Log.Main.Inform("COMPLETED:\r\nTotal files: " + processed_files + "\r\nSuccess files: " + (processed_files - failed_files.Count) + "\r\nFailed files: " + failed_files.Count + "\r\n" + string.Join("\r\n", failed_files)); if (failed_files.Count > 0) { Message.Error("There were " + failed_files.Count + " failed files.\r\nSee details in the log."); } //progress(0, 0); }
internal StorageBase(string file) { FileSystemRoutines.CreateDirectory(PathRoutines.GetFileDir(file));//done because cleanup can remove the dir this.file = file; //fs = File.Open(file, FileMode.OpenOrCreate, FileAccess.ReadWrite); }
protected Session() { This_ = this; State = SessionState.STARTING; Config.Reload(); Dir = FileSystemRoutines.CreateDirectory(Log.RootDir + "\\Data"); Directory.SetLastWriteTime(Dir, DateTime.Now);//to avoid cleaning up ConfigFile = Dir + "\\" + ConfigFileName; Restored = false; Storage = new SessionStorage(); DateTime old_start_time; string old_time_mark; SessionState old_state = Storage.ReadLastState(out old_start_time, out old_time_mark); switch (old_state) { case SessionState.NULL: break; case SessionState.STARTING: case SessionState.COMPLETED: case SessionState.FATAL_ERROR: break; case SessionState.RESTORING: case SessionState.RUNNING: case SessionState.CLOSING: case SessionState.UNCOMPLETED: case SessionState.BROKEN: case SessionState.NONFATAL_ERROR: if (Settings.Engine.RestoreBrokenSession && !CommandLine.IsParameterSet(CommandLineParameters.NOT_RESTORE_SESSION)) { if (LogMessage.AskYesNo("Previous session " + old_time_mark + " is not completed. Restore it?", true)) { StartTime = old_start_time; TimeMark = old_time_mark; Storage.WriteState(SessionState.RESTORING, new { restoring_time = RestoreTime, restoring_session_time_mark = get_time_mark(RestoreTime) }); Log.Main.Inform("Loading configuration from " + ConfigFile); Config.Reload(ConfigFile); Storage.RestoreSession(); Restored = true; } } break; default: throw new Exception("Unknown option: " + old_state); } if (!Restored) { if (old_state != SessionState.NULL) { string old_dir_new_path = Log.RootDir + "\\Data" + "_" + old_time_mark + "_" + old_state; Log.Main.Write("The old session folder moved to " + old_dir_new_path); Storage.Close(); if (Directory.Exists(Dir)) { Directory.Move(Dir, old_dir_new_path); } FileSystemRoutines.CreateDirectory(Dir); Storage = new SessionStorage(); } StartTime = Log.Head.CreatedTime;// DateTime.Now; TimeMark = get_time_mark(StartTime); Storage.WriteState(SessionState.STARTING, new { session_start_time = StartTime, session_time_mark = TimeMark }); read_input_file(); Config.Save(ConfigFile); } Creating?.Invoke(); __Creating(); }