Reputation: 10835
Is there a way to get a file count in a folder, but I want to exclude files with extension jpg?
Directory.GetFiles("c:\\Temp\\").Count();
Upvotes: 10
Views: 13556
Reputation: 2544
string[] extensions = new string[] { ".jpg", ".gif" };
var files = from file in Directory.GetFiles(@"C:\TEMP\")
where extensions.Contains((new FileInfo(file)).Extension)
select file;
files.Count();
Upvotes: 1
Reputation: 30705
You can always use LINQ.
return GetFiles("c:\\Temp\\").Where(str => !str.EndsWith(".exe")).Count();
Upvotes: 0
Reputation: 23841
Try this:
var count = System.IO.Directory.GetFiles(@"c:\\Temp\\")
.Count(p => Path.GetExtension(p) != ".jpg");
Good luck!
Upvotes: 14
Reputation: 13921
System.IO.Directory.GetFiles("c:\\Temp\\").Where(f => !f.EndsWith(".jpg")).Count();
Upvotes: 0
Reputation: 22148
You could just use a simple LINQ statement to weed out the JPGs.
Directory.GetFiles("C:\\temp\\").Where(f => !f.ToLower().EndsWith(".jpg")).Count();
Upvotes: 1
Reputation: 30820
You can use LINQ 'Where' clause to filter out files with not needed extension.
Upvotes: 0
Reputation: 3412
public static string[] MultipleFileFilter(ref string dir)
{
//determine our valid file extensions
string validExtensions = "*.jpg,*.jpeg,*.gif,*.png";
//create a string array of our filters by plitting the
//string of valid filters on the delimiter
string[] extFilter = validExtensions.Split(new char[] { ',' });
//ArrayList to hold the files with the certain extensions
ArrayList files = new ArrayList();
//DirectoryInfo instance to be used to get the files
DirectoryInfo dirInfo = new DirectoryInfo(dir);
//loop through each extension in the filter
foreach (string extension in extFilter)
{
//add all the files that match our valid extensions
//by using AddRange of the ArrayList
files.AddRange(dirInfo.GetFiles(extension));
}
//convert the ArrayList to a string array
//of file names
return (string[])files.ToArray(typeof(string));
}
Should work
Alex
Upvotes: 1
Reputation: 20296
Using Linq's Where
method:
Directory.GetFiles(path).Where(file => !file.EndsWith(".jpg")).Count();
Upvotes: 5
Reputation: 13947
You can use a DirectoryInfo
object on the directory, and do a GetFiles()
on it with a filter.
Upvotes: 6