Read all text files in a folder with StreamReader - c#

I am trying to read all .txt files in a folder using stream reader. I have this now and it works fine for one file but I need to read all files in the folder. This is what I have so far. Any suggestions would be greatly appreciated.
using (var reader = new StreamReader(File.OpenRead(#"C:\ftp\inbox\test.txt")))

You can use Directory.EnumerateFiles() method instead of.
Returns an enumerable collection of file names that match a search
pattern in a specified path.
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");
foreach (string currentFile in txtFiles)
{
...
}

You can call Directory.EnumerateFiles() to find all files in a folder.

You can retrieve the files of a directory:
string[] filePaths = Directory.GetFiles(#"c:\MyDir\");
Therefore you can iterate each file performing whatever you want. Ex: reading all lines.
And also you can use a file mask as a second argument for the GetFiles method.
Edit:
Inside this post you can see the difference between EnumerateFiles and GetFiles.
What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?

Related

Getting List of Files From Directory Into ComboBox

Using C#, I want to get the list of files in a folder.
My goal inside a combo box:
File1.txt
File2.txt
File3.txt
Process works when using Console.WriteLine however can't convert the string into the object, see below.
string[] files = Directory.GetFiles(dir);
foreach(string file in files)
ComboBox.Items.AddRange(Path.GetFileName(file));
Help would be appreciated! Thanks in advance!
If you want to stick with your foreach, you just need to change AddRange to Add:
foreach (string filePath in files) comboBox1.Items.Add(Path.GetFileName(filePath));
Here is another way using the LINQ extension method IEnumerable.Select like this:
comboBox1.Items.AddRange(files.Select((string filePath) => Path.GetFileName(filePath)).ToArray());

C# - List the name of each file in a directory into an Array?

How would one list the files in a directory into an Array? Files only, I could care less for folders. I know in python it's:
for file in os.listdir('Blah'):
#BlahBlahBlah
However, I'm not sure how I would go about doing so in C#.
Thank you for your help!
Use Directory.GetFiles method
string[] filesArray = Directory.GetFiles("yourpath");
Returns the names of files (including their paths) in the specified
directory.
Remember to include System.IO
You can also use Directory.GetFiles Method (String, String) to search files by specifying search patterns. Something like:
string[] fileArray = Directory.GetFiles(#"c:\", "X*");
return all files starting with Character X
You may use:
if(Directory.Exists("yourpath"))
to check if the path exists
using System.IO;
string[] files = Directory.GetFiles("PATH");
OR
string[] files = Directory.GetFiles("PATH","*.docx",SearchOption.AllDirectories);
OR
string[] files = Directory.GetFiles("PATH","*.pdf",SearchOption.TopDirectoryOnly);
OR
string[] files = Directory.GetFiles("PATH","*.xlsx");
Try following...Use System.IO directory
string[] filePaths = Directory.GetFiles(#"D:\MyDir\");

Searching and copying Sub Directories in C Sharp

I am currently writing a program which searches My Documents. Currently my program is able to search and copy the main my documents folder but I am unable to make it search sub directory's within the main my documents directory. I have tried multiple methods but none seem to be working out.
Currently I am using the below code to dump the files location into an array called files. sourcePath is declared in an array before hand.
string[] files = System.IO.Directory.GetFiles(sourcePath[loopcounter]);
I then have a loop which copy's the files over to another directory
foreach (string s in files)
Any help as to how to fill the array files with details of files in the sub directories of a folder would be very handy. Thanks in advance!
Use research by pattern and specify you want use recursion :
var allFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"*",
SearchOption.AllDirectories);
foreach (var item in allFiles)
{
// Do Stuff...
}
If you want details about each file, then GetFiles returns you array of names. Pass each name to FileInfo API.

How do I search for files using C# on a windows OS

I have a csv file with file names and I need to search for those filenames in a particular directory. I can read thru a csv file and get all the filenames but would like to know how can I search for those files.
Any pointers would be would of great help
What about trying following code snippet.
Directory.EnumerateFiles(directory, fileName, SearchOption.AllDirectories);
System.IO.Directory.Getfiles will give you a list of files in a particular directory. If you need to so more intensive searching. You may want to use the windows indexing.
It's pretty easy, there are many different ways you can do this. I prefer the "exists" method if I am just trying to find out if the files are there.
string SearchDirectory = "C:\\SomeDirectory\\";
List<String> FilesToSearch = new List<string>();
//Populate FilesToSearch from your csv...
foreach (String CurrentFileToSearch in FilesToSearch)
{
if (System.IO.File.Exists(SearchDirectory + TargetFileName))
{
//Do Something!
}
}

C# Directory listing massive directory

Here is the scenario:
I have a directory with 2+ million files. The code I have below writes out all the files in about 90 minutes. Does anybody have a way to speed it up or make this code more efficent? I'd also like to only write out the file names in the listing.
string lines = (listBox1.Items.ToString());
string sourcefolder1 = textBox1.Text;
string destinationfolder = (#"C:\anfiles");
using (StreamWriter output = new StreamWriter(destinationfolder + "\\" + "MasterANN.txt"))
{
string[] files = Directory.GetFiles(textBox1.Text, "*.txt");
foreach (string file in files)
{
FileInfo file_info = new FileInfo(file);
output.WriteLine(file_info.Name);
}
}
The slow down is that it writes out 1 line at a time.
It takes about 13-15 minutes to get all the files it needs to write out.
The following 75 minutes is creating the file.
It could help if you don't make a FileInfo instance for every file, use Path.GetFileName instead:
string lines = (listBox1.Items.ToString());
string sourcefolder1 = textBox1.Text;
string destinationfolder = (#"C:\anfiles");
using (StreamWriter output = new StreamWriter(Path.Combine(destinationfolder, "MasterANN.txt"))
{
string[] files = Directory.GetFiles(textBox1.Text, "*.txt");
foreach (string file in files)
{
output.WriteLine(Path.GetFileName(file));
}
}
You're reading 2+ million file descriptors into memory. Depending on how much memory you have you may well be swapping. Try breaking it up into smaller chunks by filtering on the file name.
The first thing I would need to know is, where's the slow down? is it taking 89 minutes for Directory.GetFiles() to execute or is the delay spread out over the calls to FileInfo file_info = new FileInfo(file);?
If the delay is from the latter, you can probably speed things up by getting the file name from the path instead of creating an FileInfo instance to get the filename.
System.IO.Path.GetFileName(file);
From my experience, it's Directory.GetFiles that's slowing you down (aside from console output). To overcome this, P/Invoke into FindFirstFile/FindNextFile to avoid all the memory consumption and generall lagginess.
Using Directory.EnumerateFiles do not need to load all the file names in to memory first. Check this out: C# directory.getfiles memory help
In your case, the code could be:
using (StreamWriter output = new StreamWriter(destinationfolder + "\\" + "MasterANN.txt"))
{
foreach (var file in Directory.EnumerateFiles(sourcefolder, "*.txt"))
{
output.WriteLine(Path.GetFileName(file));
}
}
From this doc, it said that:
The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.
So if you have sufficient memory, Directory.GetFiles is ok. But Directory.EnumerateFiles is much better when a folder contains millions of files.

Categories