-4

I have multiple text files saved in a folder. I want to be able to create a C# program that would read each text file, extract the info that are in the text files and then write those values to a different text file.

Here's how one of the text files look:

 line1  Material
 line2  A
 line3  Length = 129
 line4  Date
 line5  17605

I want the output text file to look something like this:

A,129,17605

Can someone provide me with a C# code that would be able to give me the output as mentioned above? Thanks!

techindustry
  • 23
  • 2
  • 11
  • Sounds like you just need to pad out the `ProcessFiles` function with some code to do the parsing of the file. Just break it up into logical steps, the code is going to be re-usable if the parsing is simple enough. – user692942 Mar 18 '20 at 14:59
  • I understand that. Can you provide a working code? – techindustry Mar 18 '20 at 15:48
  • I want to create a [*insert random programming language*] program, please write it for me. *(Was a [tag:vbscript] question now it's [been edited](https://stackoverflow.com/revisions/60741206/4) to a [tag:c#] one)*. – user692942 Mar 18 '20 at 16:17
  • 3
    Step 1: https://stackoverflow.com/questions/5840443/how-to-read-all-files-inside-particular-folder Step 2: https://stackoverflow.com/questions/7387085/how-to-read-an-entire-file-to-a-string-using-c/7387108 Step 3: https://stackoverflow.com/questions/7679601/remove-words-from-string-c-sharp Hope this helps. – ransems Mar 18 '20 at 16:26

1 Answers1

0

I write the following code example, which could save the value from different txt files to the new txt file.

Code:

class Program
    {
        static void Main(string[] args)
        {
            string path = "D:\\sample";
            var files = Directory.GetFiles(path,"*.txt");
            List<Sample> list = new List<Sample>();
            string[] text;
            foreach (var item in files)
            {
                text = File.ReadAllLines(item);
                list.Add(new Sample { Name = text[1], Number = Convert.ToInt32(text[2].Replace("Length =", "")),Id=Convert.ToInt32(text[4])});
            }

            foreach (var item in list)
            {
                text = new string[] { item.Name, item.Number.ToString(), item.Id.ToString() };
                string result = string.Join(",", text)+Environment.NewLine;
                File.AppendAllText("D:\\sample\\total.txt", result);
            }
        }


    }

    public class Sample
    { 
      public string Name { get; set; }
      public int Number { get; set; }
      public int Id { get; set; }


    }

One of text files:

Material
B
Length = 124
Date
17665

The final txt files:

enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27