5 Replies - 2743 Views - Last Post: 21 April 2012 - 06:12 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Multiple file filter with DirectoryInfo.GetFiles

Posted 21 December 2008 - 09:49 PM

Description: Need a reference to the following Namespaces: System.Collections System.IO Pass the method the directory you wish to search and retrieve files fromAs you know there really isn't an easy way to retrieve files from an extension with one of multiple extensions, say jpg, gif and png. This snippet does exactly that by using a string array of valid extensions and ArrayList to hold the files found
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));
}


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple file filter with DirectoryInfo.GetFiles

#2 Flycatcher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-September 11

Re: Multiple file filter with DirectoryInfo.GetFiles

Posted 15 September 2011 - 05:56 AM

When I tried running this, I got the following error at the line immediately preceding the return statement. At least one element in the source array could not be cast down to the destination array type. I found two solutions: (a) replace your line causing the error and the return statement as follows // return (string[])files.ToArray(typeof(string)); (Error) string[] fileNames = new string[files.Count]; for (int i = 0; i < files.Count; i++) fileNames[i] = files[i].ToString(); return fileNames; (b) change the return type from string[] to ArrayList in the method declaration then return the ArrayList files as is without conversion. I prefer the former as this second method places the onus on the caller to realise that the ArrayList returned contains FileInfo objects that need converting to strings to get the filenames.
Was This Post Helpful? 0
  • +
  • -

#3 Flycatcher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-September 11

Re: Multiple file filter with DirectoryInfo.GetFiles

Posted 15 September 2011 - 06:02 AM

(Sorry to post this twice, but I'm just finding my way around here and this is an attempt to break the lines up so the amended code is easier to read). When I tried running this, I got the following error at the line immediately preceding the return statement. At least one element in the source array could not be cast down to the destination array type. I found two solutions:
(a) replace your line causing the error and the return statement as follows
// return (string[])files.ToArray(typeof(string)); (Error)
string[] fileNames = new string[files.Count]; for (int i = 0; i < files.Count; i++) fileNames[i] = files[i].ToString();
return fileNames;

(b) change the return type from string[] to ArrayList in the method declaration then return the ArrayList files as is without conversion. I prefer the former as this second method places the onus on the caller to realise that the ArrayList returned contains FileInfo objects that need converting to strings to get the filenames.
Was This Post Helpful? 0
  • +
  • -

#4 Flycatcher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-September 11

Re: Multiple file filter with DirectoryInfo.GetFiles

Posted 15 September 2011 - 06:03 AM

... and it failed. Anyone tell me how to format these comments with line breaks so they are more readable please?
Was This Post Helpful? 0
  • +
  • -

#5 Flycatcher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-September 11

Re: Multiple file filter with DirectoryInfo.GetFiles

Posted 15 September 2011 - 06:29 AM

OK, sorry again - last try. When I tried running this, I got the following error at the line immediately preceding the return statement. At least one element in the source array could not be cast down to the destination array type. I found two solutions: (a) replace your line causing the error and the return statement as follows

// return (string[])files.ToArray(typeof(string)); (Error) 
string[] fileNames = new string[files.Count];
for (int i = 0; i < files.Count; i++) 
    fileNames[i] = files[i].ToString(); 
return fileNames; 

(b) change the return type from string[] to ArrayList in the method declaration then return the ArrayList files as is without conversion. I prefer the former as this second method places the onus on the caller to realise that the ArrayList returned contains FileInfo objects that need converting to strings to get the filenames.
Was This Post Helpful? 0
  • +
  • -

#6 marekbar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 21-April 12

Re: Multiple file filter with DirectoryInfo.GetFiles

Posted 21 April 2012 - 06:12 AM

I changed that code this way:

public static string [] fileFilter(string extensions, string dir )
	{
		string [] extensionsArray = extensions.Split(new char [] { ',' });
		var files = new ArrayList();

		foreach (var ext in extensionsArray)
		{
			FileInfo [] fileInfos = new DirectoryInfo(dir).GetFiles("*." + ext);
			foreach (var file in fileInfos)
			{
				files.Add(file.Name);
			}
		}
		return (string [])files.ToArray(typeof(string));
	}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1