using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using IO = System.IO; namespace MLMStripper { class Program { static string INSERT_SQL = "INSERT [dbo].[CV3MLM] ([SiteID], [RepFlags], [Build], [TouchedBy], [TouchedWhen], [CreatedBy], [CreatedWhen], [Active], [GUID], [ID], [Name], [Description], [Status], [Logic], [MSrepl_tran_version], [UsageType], [Title], [ArdenVersion], [MLMVersion], [MLMDate], [MSReplrowguid]) VALUES ("; // # Readin and Process all Lines static string curTitle; static List ReadInLines = new List(); static bool LinesInBuffer { get { return ReadInLines.Count > 0; } } #region Simple Getters static string GetTitle(string line) { string s = line.Substring(INSERT_SQL.Length); string[] split = s.Split(','); string title = split[11].Trim().Substring(2); // removing starting 'N and last ' return title.Substring(0, title.Length - 1); } static int GetStatus(string line) { string s = line.Substring(INSERT_SQL.Length); string[] split = s.Split(','); int status = int.Parse(split[13].Trim()); return status; } static int GetActive(string line) { string s = line.Substring(INSERT_SQL.Length); string[] split = s.Split(','); int active = int.Parse(split[7].Trim()); return active; } #endregion static int nPosFindIn(string startswithToLower, List contents) { for (int i = contents.Count - 1; i >= 0; i--) { string s = contents[i].Trim(); if (s.ToLower().StartsWith(startswithToLower.ToLower())) { return i; } } return -1; } static void CleanupEndOfFile(List contents) { bool bAddEnd = false; int nPos = nPosFindIn("end:", contents); if (nPos == -1) { nPos = nPosFindIn("urgency:", contents); bAddEnd = true; } if (nPos == -1) { for (int i = contents.Count - 1; i >= 0; i--) { string s = contents[i].Trim(); if (string.IsNullOrEmpty(s)) { contents.RemoveAt(i); } else { break; } } } else { contents.RemoveRange(nPos + 1, (contents.Count - nPos - 1)); } if (bAddEnd) contents.Add("end:"); } static void WriteOutFile() { // write out contents - remove last line and cleanup end ReadInLines.RemoveAt(ReadInLines.Count - 1); CleanupEndOfFile(ReadInLines); // Now do the actual work string dirpath = IO.Directory.GetCurrentDirectory() + "\\" + curTitle.Split('_')[0]; if (!IO.Directory.Exists(dirpath)) IO.Directory.CreateDirectory(dirpath); string FileNameNPath = dirpath + "\\" + curTitle + ".mlm"; if(IO.File.Exists(FileNameNPath)) { int i = 44; } using (IO.FileStream fs = new IO.FileStream(FileNameNPath, IO.FileMode.Create)) using (IO.StreamWriter sw = new IO.StreamWriter(fs, Encoding.UTF8)) { foreach (string line in ReadInLines) sw.WriteLine(line); sw.Flush(); } // done ReadInLines.Clear(); curTitle = String.Empty; } static void Main(string[] args) { if (args == null || string.IsNullOrEmpty(args[0])) { Console.WriteLine("Provide Filename"); return; } string Filename = args[0]; if (!IO.File.Exists(Filename)) { Console.WriteLine("File does not exist"); return; } // # Readin and Process all Lines using (IO.FileStream fs = new IO.FileStream(Filename, IO.FileMode.Open)) using (IO.StreamReader reader = new IO.StreamReader(fs, Encoding.UTF8)) { // Ignore the first two lines reader.ReadLine(); reader.ReadLine(); int filecount = 0; // # Iterate thru all the rest of the lines in the File for (string Line = reader.ReadLine(); Line != null; Line = reader.ReadLine()) { if (Line.StartsWith(INSERT_SQL)) { if (LinesInBuffer) { WriteOutFile(); } // we need active and status of 4 int active = GetActive(Line); int status = GetStatus(Line); //Trace.WriteLine(String.Format("Active:{0} - Status:{1} - FileCount:{2}", active, status, ++filecount)); if (active != 1 || status != 4) continue; // Begin Parsing this file curTitle = GetTitle(Line); //if (curTitle == "SCH_OUTPATIENT_STATUS_BOARD_COLUMN_UPDATE_2") //{ // int i = 44; //} Trace.WriteLine(String.Format("{0} - FileCount:{1}", curTitle, ++filecount)); // clear buffer ReadInLines.Clear(); ReadInLines.Add("maintenance:\n"); // begining new file continue; } else if (!String.IsNullOrEmpty(curTitle)) { ReadInLines.Add(Line); } } if (LinesInBuffer) { WriteOutFile(); } } } } }