I have splitted files, from the main file.
The main file looks like this:
Y, ,09/30/16 10:48:40,c,1,.84054972,0,P,0,0,30,0,1,1,0,20
X,1, ,09/30/16 10:48:40,c,b,,r,,F,,,,,,
END
Y, ,09/30/16 10:48:40,c,1,.84054972,0,P,0,0,30,0,1,1,0,20
X,1, ,09/30/16 10:48:40,c,b,,r,,F,,,,,,
END
and i am splitting this file into many files depending on the string END. So in this case it will be splitted into two Files.
The name of the two splitted files are:
1.rslt.09-30-2016.10.17.43
1.rslt.09-30-2016.10.39.38
Now i want to take the bold part from the name of the file which is called date. and put it inside the splitted file in these locations
Y, ,Date,c,1,.84054972,0,P,0,0,30,0,1,1,0,20
X,1, ,Date,c,b,,r,,F,,,,,,
END
which means location 2 incase of the header and location 3 incase of normal line. I have the snippet for the split and while i am writting the file i am copying location 1 which has the value 1 into all upper columns. How can i write Date into locations 2 and 3 at the same time when i am writing the file?
The string date in the snippet is that date i want to put which is taken from the name of the file.
- var templines = File.ReadAllLines(SomeGlobalVariables.tempfilepath).ToList();
- int currentLine = 0;
- //string track = "PATS_TRACK";
- //string test = "PATS_TEST";
- while (currentLine < templines.Count)
- {
- foreach (var date in dates)
- {
- // Peek ahead for the END-OF-LINE to get the name of the file.
- int terminatorLine = templines.IndexOf("END-OF-LINE", currentLine);
- if (terminatorLine <= 0) break; // If not found, or no penultimate line
- int penultimateLine = terminatorLine - 1;
- var fields2 = templines[penultimateLine].Split(','); // get the comma separated fields // Last Line
- var fields3 = templines[penultimateLine-1].Split(','); // Line before last one
- name_1 = string.Format(@"C:\Users\{0}" + "." + "rslt" + "." + date, fields2[1]); // make a name out of the second field.
- if (fields2[0].Equals("X") && fields3[0].Equals("Y"))
- {
- fields2[3] = SwapCharacters(SwapCharacters(date.Replace("-", "/").Replace(".", ":").Remove(10, 1).Insert(10, " "), 6, 8), 7, 9).Remove(8, 2); // incase of last line
- fields3[2] = SwapCharacters(SwapCharacters(date.Replace("-", "/").Replace(".", ":").Remove(10, 1).Insert(10, " "), 6, 8), 7, 9).Remove(8, 2); // Lines before
- // Console.WriteLine(fields2[3]);
- Console.WriteLine(fields3[2]);
- }
- File.WriteAllLines(name_1, templines.Skip(currentLine).Take(terminatorLine - currentLine).Select(line => string.Join(",", line.Split(',').Select((text, index) => (index == 1) ? fields2[1] : text)))); // copy the lines.
- // File.WriteAllLines(name_1, templines.Skip(currentLine));
- currentLine = terminatorLine + 1;
- }
- }
Madhanmohan DevarajanPosted Oct 5, 2016, 9:52 AM
Madhanmohan DevarajanPosted Oct 5, 2016, 8:39 AM