Results 1 to 17 of 17

Thread: Read Multiple Text files Into Master Text File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Read Multiple Text files Into Master Text File

    I have multiple text files in sub-directories of R:\Text\ - I want to take the data in each individual file and compile one master file that has all the data that the subsets contain. What would be the best way for me to do this using C#?

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Read Multiple Text files Into Master Text File

    Best way to do it would be to get a list of all .txt files in the directory you want, and loop through them.

    Open each file and read the contents. Depending on the number of files and the size of the files, you could read them all into one string variable and then print that variable to your master text file.

    If the files are large in size or you have a lot of them, it would be better to open the master file for write, write the file to it, and then close it, however that would be slower.

    If you have any issues along the way, feel free to post some code and we can try to help.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Read Multiple Text files Into Master Text File

    They are small files (IMO)...I just looked through a few of them and the largest I saw was 5KB.

    There are close to 180 files that I need to open and perform this action on. With that # which approach (from your suggestions above) would you recommend?

  4. #4
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Read Multiple Text files Into Master Text File

    Do you really want to use C# for this trivial task? It would be easier to do this in powershell like this.

    Code:
    cat example1.txt, example2.txt > examples.txt
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by abhijit View Post
    Do you really want to use C# for this trivial task? It would be easier to do this in powershell like this.

    Code:
    cat example1.txt, example2.txt > examples.txt
    How could I get a listing of all the .txt files in the directory/sub-directory with powershell?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by Jo15765 View Post
    They are small files (IMO)...I just looked through a few of them and the largest I saw was 5KB.

    There are close to 180 files that I need to open and perform this action on. With that # which approach (from your suggestions above) would you recommend?
    @KFCSmitty --- I think I am close to what I am after. But I am not clear on how to take the data from the existing and paste into a 'master'.
    Code:
    public static void DoThis()
    {
      string[] filePaths = Directory.GetFiles("", "*txt", SearchOptions.AllDirectories);
      foreach (string fr in filePaths)
      {
         int counter = 0;
         string line;
         System.IO.StreamReader file = new System.IO.StreamReader(fr);
         while ((line = file.ReadLine()) != null)
         {
            //here is where the write to master would go
            counter++;
          }
          file.Close();
        }
    }

  7. #7
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by Jo15765 View Post
    How could I get a listing of all the .txt files in the directory/sub-directory with powershell?
    Get-ChildItem cmdlet is what you want.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  8. #8
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by Jo15765 View Post
    @KFCSmitty --- I think I am close to what I am after. But I am not clear on how to take the data from the existing and paste into a 'master'.
    Code:
    public static void DoThis()
    {
      string[] filePaths = Directory.GetFiles("", "*txt", SearchOptions.AllDirectories);
      foreach (string fr in filePaths)
      {
         int counter = 0;
         string line;
         System.IO.StreamReader file = new System.IO.StreamReader(fr);
         while ((line = file.ReadLine()) != null)
         {
            //here is where the write to master would go
            counter++;
          }
          file.Close();
        }
    }
    You don't need to read the file line by line. You can do a File.ReadAlltext
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  9. #9
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Read Multiple Text files Into Master Text File

    In your foreach, it would probably be easier to use the File.ReadAllText() method to read the contents of an entire file into a string variable, and then you could use File.AppendAllText() to append the data you just read onto the master file.

    The documentation for ReadAllText can be found at:

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    And AppendAllText can be found at:

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by Jo15765 View Post
    How could I get a listing of all the .txt files in the directory/sub-directory with powershell?
    I looked into this and it is very intriguing to me. It seems that the syntax I would need is this? But the directory's I want to search are not at C:\ so how could I change that or set the correct directory (R:\Text\)?
    Code:
    PS C:\> Get-ChildItem - Path *.txt -Recurse -Force

  11. #11
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by Jo15765 View Post
    I looked into this and it is very intriguing to me. It seems that the syntax I would need is this? But the directory's I want to search are not at C:\ so how could I change that or set the correct directory (R:\Text\)?
    Code:
    PS C:\> Get-ChildItem - Path *.txt -Recurse -Force
    Instead of path, you could give your folder path like this.

    Code:
    Get-ChildItem c:\app\ *.txt -Recurse
    Alternatively, you could also do a

    Code:
    cd c:\app
    Either syntax is perfectly legal.

    If you wanted to loop them, then use this.

    Code:
    Get-ChildItem c:\app\ *.txt -Recurse | ForEach-Object {Write-Host $_.name}
    Last edited by abhijit; Sep 3rd, 2014 at 01:53 PM.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by kfcSmitty View Post
    In your foreach, it would probably be easier to use the File.ReadAllText() method to read the contents of an entire file into a string variable, and then you could use File.AppendAllText() to append the data you just read onto the master file.

    MUCH shorter code, but I am still do not know how to generate the 'master' text file that the results of readText are going to be written to each iteration?
    EDIT ----
    In my code below npd2 is what I would use as my 'master' text file instance, if I can figure out how to generate one, haha
    End Of Edit -----

    Code:
    public static void DoThis()
    {
    	string[] filePaths = Directory.GetFiles("", "*txt", SearchOptions.AllDirectories);
    	foreach (string fr in filePaths)
    	{
    		string readText = File.ReadAllText(fr);
    		File.AppendAllText(npd2, readText);
    	}
    }

  13. #13
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Read Multiple Text files Into Master Text File

    In your above code, you need to check for File existence. Look at this example.

    Code:
     if (!File.Exists(path))
            {
                // Create a file to write to. 
                string createText = "Hello and Welcome" + Environment.NewLine;
                File.WriteAllText(path, createText);
            }
    
            // This text is always added, making the file longer over time 
            // if it is not deleted. 
            string appendText = "This is extra text" + Environment.NewLine;
            File.AppendAllText(path, appendText);
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  14. #14
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Read Multiple Text files Into Master Text File

    This code is doing the job for me.

    Code:
    Get-ChildItem c:\app\ *.txt -Recurse | ForEach-Object {get-content $_.FullName >> c:\temp\report.txt}
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Read Multiple Text files Into Master Text File

    Ah - so close. My final code was below
    Code:
    public static void DoThis()
    {
    	string path = "F:\\MasterTemplate.txt;
    	string[] filePaths = Directory.GetFiles("", "*txt", SearchOptions.AllDirectories);
    	foreach (string fr in filePaths)
    	{
    		if (File.Exists(path))
    		{
    			string readText = File.ReadAllText(fr);
    			File.AppendAllText(path, readText);
    		}
    	}
    }
    Last edited by Jo15765; Sep 3rd, 2014 at 02:28 PM.

  16. #16
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: Read Multiple Text files Into Master Text File

    where are you declaring npd2?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Read Multiple Text files Into Master Text File

    Quote Originally Posted by abhijit View Post
    where are you declaring npd2?
    Copy/Paste error on my part. I have updated my above syntax.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width