2
Answers

Writing into a text file simultaneously

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.

  1. var templines = File.ReadAllLines(SomeGlobalVariables.tempfilepath).ToList();  
  2.            int currentLine = 0;  
  3.            //string track = "PATS_TRACK";  
  4.            //string test = "PATS_TEST";  
  5.            while (currentLine < templines.Count)  
  6.            {  
  7.                foreach (var date in dates)  
  8.                {   
  9.                // Peek ahead for the END-OF-LINE to get the name of the file.  
  10.                int terminatorLine = templines.IndexOf("END-OF-LINE", currentLine);  
  11.                if (terminatorLine <= 0) break// If not found, or no penultimate line  
  12.                int penultimateLine = terminatorLine - 1;  
  13.                var fields2 = templines[penultimateLine].Split(','); // get the comma separated fields // Last Line  
  14.   
  15.                var fields3 = templines[penultimateLine-1].Split(','); // Line before last one  
  16.   
  17.                name_1 = string.Format(@"C:\Users\{0}" + "." + "rslt" + "." + date, fields2[1]); // make a name out of the second field.  
  18.                if (fields2[0].Equals("X") && fields3[0].Equals("Y"))  
  19.                {  
  20.                    fields2[3] = SwapCharacters(SwapCharacters(date.Replace("-""/").Replace("."":").Remove(10, 1).Insert(10, " "), 6, 8), 7, 9).Remove(8, 2); // incase of last line  
  21.                    fields3[2] = SwapCharacters(SwapCharacters(date.Replace("-""/").Replace("."":").Remove(10, 1).Insert(10, " "), 6, 8), 7, 9).Remove(8, 2); // Lines before  
  22.                 //  Console.WriteLine(fields2[3]);  
  23.                   Console.WriteLine(fields3[2]);   
  24.                }  
  25.                  
  26.                  
  27.               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.      
  28.           //   File.WriteAllLines(name_1, templines.Skip(currentLine));  
  29.                currentLine = terminatorLine + 1;  
  30.   
  31.                }  
  32.              
  33.            }  
Answers (2)