Reputation: 1157
I have a FileInfo array, which returns me 15 files and one ".zip" folder. I basically need to try and somehow filter this ".zip" file out of the array. Any help would be much appreciated, but it seems I am just banging my head against a brick wall! Here is the code that I am trying to impliment this into;
public List<FileInfo> getInfo(bool recursive, int ageDays)
{
//Declarations
DirectoryInfo di = new DirectoryInfo(CurrentFilePath);
FileInfo[] fi = new FileInfo[0];
List<FileInfo> results = new List<FileInfo>();
fileResults = new List<Files>();
DateTime ageInDays = DateTime.Now.AddDays(-ageDays);
//Checks for recursive search
if (recursive)
{
try
{
fi = di.GetFiles("*.*", SearchOption.AllDirectories);
}
catch (Exception)
{
}
}
else
{
try
{
fi = di.GetFiles();
}
catch (Exception)
{
}
}
for (int i = 0; i < fi.Length; i++)
{
if (fi[i].LastWriteTime < ageInDays)
{
results.Add(fi[i]);
}
}
return results;
}
Thanks in advance!
Upvotes: 0
Views: 2328
Reputation: 140
You can also use lambda to check the extension of your file
var files = di.GetFiles().Where(file => !file.FullName.EndsWith(".zip"));
Upvotes: 0
Reputation: 7961
Best to change your code:
for (int i = 0; i < fi.Length; i++)
{
if ((fi[i].LastWriteTime < ageInDays) && fi.Extension.ToUpper() != ".ZIP")
{
results.Add(fi[i]);
}
}
Or use LINQ:
results = (from fi in results
where fi.Extension.ToUpper() != ".ZIP"
select fi).ToList<FileInfo>();
Upvotes: 1
Reputation: 4652
try this:
_files = Directory.GetFiles(FTPOutputDirectory);
foreach (string fi in _files)
{
string fi = Path.GetExtension(f);
if (fi.ToUpper() != ".ZIP" )
{
// yourWork
}
}
Upvotes: 0
Reputation: 33857
Have you tried:
for (int i = 0; i < fi.Length; i++)
{
if (fi[i].LastWriteTime < ageInDays && fi[i].Extension != ".zip")
{
results.Add(fi[i]);
}
}
Upvotes: 1