Note that there are some explanatory texts on larger screens.

plurals
  1. POCode to reformat file
    primarykey
    data
    text
    <p>Need help formatting a seperated .txt file in C#. I have a text file that contains a directory listing and looks like as follows when I open up in notepad or ultra-edit. First column is date and time, next column is the size of file in bytes, third column is the username and fourth column is the name of the file. Each column is separated by one or more spaces, and the filename column at the end can contain spaces in the filename. They consist of more directories and the total amount of lines in the file is about 200,000. </p> <blockquote> <p>&nbsp;Directory of V:\word <br> 01/10/2013&nbsp;&nbsp;&nbsp;12:30 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;23,000&nbsp;&nbsp;&nbsp;BUILTIN/ADMINISTRATOR&nbsp;&nbsp;&nbsp;&nbsp; FILE&nbsp;NAME.XLS <BR> 10/25/2013&nbsp;&nbsp;&nbsp;10:39 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1,332,432 &nbsp;&nbsp;AMERICAS/DOEJ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;FILENAME2.CSV <BR> 11/31/2000&nbsp;&nbsp;&nbsp;09:54 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;21,999,999 &nbsp;&nbsp;AMERICAS/DOEF&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F_I_L_E_N_A_M_E_4.PDF <BR> &nbsp;Directory of V:\word\administrators <br> 01/10/2013&nbsp;&nbsp;&nbsp;12:30 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;23,000&nbsp;&nbsp;&nbsp;BUILTIN/ADMINISTRATOR&nbsp;&nbsp;&nbsp;&nbsp; FILENAME.XLS <BR> 10/25/2013&nbsp;&nbsp;&nbsp;10:39 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1,332,432 &nbsp;&nbsp;AMERICAS/DOEJ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;FILENAME2.CSV <BR> 11/31/2000&nbsp;&nbsp;&nbsp;09:54 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;21,999,999 &nbsp;&nbsp;AMERICAS/DOEF&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F_I_L_E_N_A_M_E_4.PDF <BR></p> </blockquote> <p>My goal is to try and add the path of the directory (ex. V:\Word or other directories) in a fixed format at the end of the filename. So Once you see the "Directory V:\word" then you know every line after and up until a new Directory, should show that path at the end of the filename. This would be considered the fifth column.</p> <p>Here is some code, but I still need to help. I am able to get V:\word at the end of the file, but how do I read the new directory and append that to the end of the lines for all subsequent lines? </p> <pre><code>private void button1_Click(object sender, EventArgs e) { var sbText = new StringBuilder(10000); string currLine = " Directory of V:\\word "; try { using (StreamReader Reader = new StreamReader(@"C:\V.txt")) { while (!Reader.EndOfStream) { if (currLine != " Directory of V:\\word ") { MessageBox.Show("No Directory"); } else { sbText.AppendLine(Reader.ReadLine() + "V:\\word"); } } // When all of the data has been loaded, write it to the text box in one fell swoop richTextBox1.Text = sbText.ToString(); using (StreamWriter Writer = new StreamWriter(@"C:\NEWFILE.txt")) { Writer.WriteLine(sbText); } } } catch (Exception ex) { MessageBox.Show("An error has occured. " + ex.Message); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload