-
Sep 3rd, 2014, 08:06 AM
#1
Thread Starter
Hyperactive Member
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#?
-
Sep 3rd, 2014, 11:59 AM
#2
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.
-
Sep 3rd, 2014, 12:12 PM
#3
Thread Starter
Hyperactive Member
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?
-
Sep 3rd, 2014, 12:59 PM
#4
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
-
Sep 3rd, 2014, 01:07 PM
#5
Thread Starter
Hyperactive Member
Re: Read Multiple Text files Into Master Text File
Originally Posted by abhijit
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?
-
Sep 3rd, 2014, 01:11 PM
#6
Thread Starter
Hyperactive Member
Re: Read Multiple Text files Into Master Text File
Originally Posted by Jo15765
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();
}
}
-
Sep 3rd, 2014, 01:33 PM
#7
Re: Read Multiple Text files Into Master Text File
Originally Posted by Jo15765
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
-
Sep 3rd, 2014, 01:34 PM
#8
Re: Read Multiple Text files Into Master Text File
Originally Posted by Jo15765
@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
-
Sep 3rd, 2014, 01:44 PM
#9
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
-
Sep 3rd, 2014, 01:45 PM
#10
Thread Starter
Hyperactive Member
Re: Read Multiple Text files Into Master Text File
Originally Posted by Jo15765
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
-
Sep 3rd, 2014, 01:50 PM
#11
Re: Read Multiple Text files Into Master Text File
Originally Posted by Jo15765
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
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
-
Sep 3rd, 2014, 01:51 PM
#12
Thread Starter
Hyperactive Member
Re: Read Multiple Text files Into Master Text File
Originally Posted by kfcSmitty
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);
}
}
-
Sep 3rd, 2014, 02:02 PM
#13
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
-
Sep 3rd, 2014, 02:22 PM
#14
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
-
Sep 3rd, 2014, 02:22 PM
#15
Thread Starter
Hyperactive Member
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.
-
Sep 3rd, 2014, 02:23 PM
#16
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
-
Sep 3rd, 2014, 02:28 PM
#17
Thread Starter
Hyperactive Member
Re: Read Multiple Text files Into Master Text File
Originally Posted by abhijit
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|