using MergeCMInpro.Data; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace MergeCMInpro.Business { public static class FileOperations { public static string[] ExtractFilePath(string path) { var splittedAbsolutePath = path.Split("\\").SkipLast(2).LastOrDefault(); return splittedAbsolutePath.Split(" "); } /// /// Kopiert Quelldateien an Zielort /// /// Quellpfad /// Zielordner /// Zielname /// Zielpfad public static void CopySourcePdfFileToDestination(string sourceFilePath, string destFolderName, string destFileName, string archivNetPath) { string destPath = Path.Combine(archivNetPath, destFolderName); if (!Directory.Exists(destPath)) Directory.CreateDirectory(destPath); File.Copy(sourceFilePath, Path.Combine(destPath, destFileName), true); } public static void CopyJPMFilesToDestination(string sourcePath, string archivNetPath) { string pattern = "(\\.)"; if (Directory.GetDirectories(sourcePath).Length > 0 && Directory.GetFiles(sourcePath, "*.jpm").Length == 0) { foreach (var item in Directory.GetDirectories(sourcePath)) { CopyJPMFilesToDestination(item, archivNetPath); } } else { var temp = sourcePath.Split("\\"); string extractedPath = string.Empty; string archivPath = Path.Combine(archivNetPath, "Archiv"); foreach (var item in temp) { if (Regex.Match(item, pattern).Success) { extractedPath = string.Concat("Sicherungen jpm ", item.Split(" ").FirstOrDefault(), " bis ", item.Split(" ").ElementAt(2)); break; } } if (!Directory.Exists(Path.Combine(archivPath, extractedPath))) { Directory.CreateDirectory(Path.Combine(archivPath, extractedPath)); } int count = 0; foreach (var t in Directory.GetFiles(sourcePath, "*.jpm")) { FileInfo fi = new FileInfo(t); if (!File.Exists(Path.Combine(archivPath, extractedPath, fi.Name))) { File.Copy(t, Path.Combine(archivPath, extractedPath, fi.Name)); count++; Console.Write("\r{0} files copied", count); } else { Console.Write("\rskipped!"); } } } } public static DateTime GetCreationDate(string file) { FileInfo fi = new FileInfo(file); return fi.LastWriteTime; } public static string GetFullFilePath(string file, string csvPath) { string currentDirectory = Directory.GetParent(csvPath).FullName; string res = string.Empty; res = Directory.GetFiles(currentDirectory, file, SearchOption.AllDirectories).FirstOrDefault(); if (res == null) return string.Empty; else { return res; } } //Splittet Informationen der Zeilen in Dienstleister-Datei (Rohdaten) public static void SplitRow(string path, string archivNetPath) { int count = 0; int countRows = File.ReadAllLines(path).Length; using (StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("ISO-8859-1"))) { string[] arr = new string[10]; while (!sr.EndOfStream) { count++; if (count != countRows) { Console.Write("\r{0}/{1} ", count, countRows); } else { Console.WriteLine("\r{0}/{1} ", count, countRows); } arr = sr.ReadLine().Split(";"); //Erstelle Basis-Vorgang aus den SAGA-Daten Vorgang v; if(arr[2].Contains("70/")) { //arr[0]=ID z.B. 815922, arr[2]=AZ z.B. 61/5-3-048105, arr[3]=Strasse z.B. Westheck, arr[5]=DocType z.B. SCHRIFTVERKEHR, arr[6]=Teil z.B. 001, arr[8]=PDF z.B. 815922.pdf v = new Vorgang(arr[0], arr[2], string.Concat(arr[5], " Teil ", arr[6]), arr[6], arr[8], arr[3],arr[4]); } else { v = new Vorgang(arr[0], arr[2], string.Concat(arr[5], " Teil ", arr[6]), arr[6], arr[8]); } #region PDF v.AbsoluteFilePath = GetFullFilePath(arr[8], path); //arr[7]=jpm-Dateien if (v.AbsoluteFilePath == string.Empty) { Console.WriteLine("Keine PDF-Dateien gefunden."); return; } v.CreationDate = GetCreationDate(v.AbsoluteFilePath); #endregion //Ergänze Basis-Vorgang mit den Daten aus der DB DataOperations.ConnectAndExecute(v, archivNetPath); } } } public static string LowerDocumentType(string value) { switch (value) { case "SCHRIFTVERKEHR": value = "01"; break; case "ZEICHNUNGEN": value = "02"; break; case "STATIK": value = "03"; break; case "SONSTIGES": value = "04"; break; case "GENEHMIGUNG": value = "05"; break; default: break; } return value; } } }