Answered by:
Get multiple files with different extension

Question
-
hi, as i know that method GetFiles() cannot get multiple extension, so is there a way that i could get files with multiple extension?
Rather than as below which it only will get all files with .txt extension:
FileInfo[] fiArr = Folder1.GetFiles("*.txt");
kok loongSunday, June 5, 2011 10:08 AM
Answers
-
You can't set more search filters, because GetFiles only accepts a single search pattern. Instead, you can call GetFiles with no pattern, and filter the results in code, using Lambda expressions:
public YourMethodToStart() { FileInfo[] fiels = GetFilesFromFolder(@"C:\1"); } private FileInfo[] GetFilesFromFolder(string path) { string[] extensions = new[] { ".txt", ".zip", ".exe" }; DirectoryInfo dInfo = new DirectoryInfo(path); FileInfo[] files = dInfo.GetFiles() .Where(f => extensions.Contains(f.Extension.ToLower())) .ToArray(); return files; }
Mitja- Marked as answer by kokloong Monday, June 6, 2011 12:30 AM
Sunday, June 5, 2011 4:24 PM
All replies
-
hi, as i know that method GetFiles() cannot get multiple extension, so is there a way that i could get files with multiple extension?
Rather than as below which it only will get all files with .txt extension:
FileInfo[] fiArr = Folder1.GetFiles("*.txt");
kok loong
Hello kokloong,you can use FileInfo[] fiArr = Folder1.GetFiles("*.*");
Hello
Carmelo La Monica http://community.visual-basic.it/carmelolamonica/Sunday, June 5, 2011 10:15 AM -
hi carmelo, i get it. but how about if i only want to get 2 extension only? maybe i would like to get files with extension .txt and .bmp. can this be done?
kok loongSunday, June 5, 2011 10:18 AM -
While GetFiles is good for this purpose, .NET 4 comes with a more efficient method EnumerateFiles().
Agility. http://salakoahmed.blogspot.com- Proposed as answer by Krishnakant Mahamuni Sunday, June 5, 2011 2:18 PM
Sunday, June 5, 2011 10:29 AM -
hi carmelo, i get it. but how about if i only want to get 2 extension only? maybe i would like to get files with extension .txt and .bmp. can this be done?
kok loong
Hello kokloong,private void button1_Click(object sender, EventArgs e) { FileInfo[] fiArr = Folder1.GetFiles("(*.*)|*.*|(*.bmp)|*.bmp"); }
Hello
Carmelo La Monica http://community.visual-basic.it/carmelolamonica/Sunday, June 5, 2011 10:32 AM -
Hi Carmelo, I tried but error occured (ArgumentException was unhandled - Illegal characters in path).
FileInfo[] fiArr = Folder1.GetFiles("(*.txt)|*.txt|(*.bmp)|*.bmp"); FileInfo[] fiArr2 = Folder2.GetFiles("(*.txt)|*.txt|(*.bmp)|*.bmp");
kok loongSunday, June 5, 2011 2:58 PM -
On 6/5/2011 6:32 AM, Carmelo La Monica wrote:> hi carmelo, i get it. but how about if i only want to get 2> extension only? maybe i would like to get files with extension .txt> and .bmp. can this be done?> ------------------------------------------------------------------------> kok loong>>> Hello kokloong,>> private void button1_Click(object sender, EventArgs e)> {> FileInfo[] fiArr = Folder1.GetFiles("(*.*)|*.*|(*.bmp)|*.bmp");> }>> Hello> ------------------------------------------------------------------------> Carmelo La Monica http://community.visual-basic.it/carmelolamonica/Just comeback with all the files in the array and filter with Linq ifusing VS 2008 or better.var files = (from a in fiArr where a.EndsWith("bmp") ||n.EndsWith("txt") select a).ToArray();foreach(var filename in files){string thename = filename;}
- Proposed as answer by Carmelo La Monica Sunday, June 5, 2011 3:46 PM
Sunday, June 5, 2011 3:19 PM -
hi carmelo, i get it. but how about if i only want to get 2 extension only? maybe i would like to get files with extension .txt and .bmp. can this be done?
kok loong
Hello kokloong,I agree with what is written by darnold924,
I tried this code then writes the result inside a RichTextBox:private void button1_Click(object sender, EventArgs e) { DirectoryInfo folder = new DirectoryInfo(@"J:\"); FileInfo[] fiArr = folder.GetFiles("*.*"); var files = from a in fiArr where a.ToString().EndsWith(".txt") || a.ToString().EndsWith(".bmp") select a; foreach (var filename in files) { richTextBox1.Text = richTextBox1.Text + filename + "'\n'"; } }
PS: if this post help you find and mark the right and proper response
darnold924, as I prepared the sample in his suggestion.Hello kokloong and Thanks darnold934
Carmelo La Monica http://community.visual-basic.it/carmelolamonica/
Sunday, June 5, 2011 3:52 PM -
You can't set more search filters, because GetFiles only accepts a single search pattern. Instead, you can call GetFiles with no pattern, and filter the results in code, using Lambda expressions:
public YourMethodToStart() { FileInfo[] fiels = GetFilesFromFolder(@"C:\1"); } private FileInfo[] GetFilesFromFolder(string path) { string[] extensions = new[] { ".txt", ".zip", ".exe" }; DirectoryInfo dInfo = new DirectoryInfo(path); FileInfo[] files = dInfo.GetFiles() .Where(f => extensions.Contains(f.Extension.ToLower())) .ToArray(); return files; }
Mitja- Marked as answer by kokloong Monday, June 6, 2011 12:30 AM
Sunday, June 5, 2011 4:24 PM -
Hi all,
Thank you very much for giving me so many ways to solve my problem.
kok loongMonday, June 6, 2011 12:48 AM