Directory.GetFiles() Search Pattern

Excluding files with a certain extension

Page 1 of 1

5 Replies - 33947 Views - Last Post: 25 January 2010 - 01:27 PM Rate Topic: -----

#1 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Directory.GetFiles() Search Pattern

Posted 21 January 2010 - 04:01 PM

Does anyone know if it is possible to EXCLUDE file extension(s) from a search?

I have a folder with *.apsim, *.sim, *.met, *.ini and various other file extensions which may or may not be known to the program at the time. I want to be able to get all the files, except the *.apsim ones.

I know I could just get all the files, then remove the *.apsim ones manually, it's just a bit more elegant if I could exclude them in the first place.

Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Directory.GetFiles() Search Pattern

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Directory.GetFiles() Search Pattern

Posted 21 January 2010 - 04:18 PM

Regular expressions may contain the special character ^ which is the negation of the expression following. I don't now if the method takes regular expressions or not.
Was This Post Helpful? 0
  • +
  • -

#3 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Directory.GetFiles() Search Pattern

Posted 21 January 2010 - 05:13 PM

Quote

Does anyone know if it is possible to EXCLUDE file extension(s) from a search?


When you are talking about a "search", do you mean the folder browser dialog box? Or is this something else entirely?
If it's the FolderBrowserDialog then there's a filter property.

http://msdn.microsof...wserdialog.aspx
Was This Post Helpful? 0
  • +
  • -

#4 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Directory.GetFiles() Search Pattern

Posted 21 January 2010 - 05:23 PM

Ok I solved it. Sorry Dark_Nexus but '^' doesn't seem to work (it was, however, the one 'NOT' that I hadn't yet tried). Using .NET 2.0, it looks like the only way you can exclude files by extension in one line is by using 'Array.FindAll' (I could be wrong, feel free to correct me).

Code:
void MyMethod(string tempjobfolder)
{
string[] allfiles = Array.FindAll(Directory.GetFiles(tempjobfolder), IsNOTdotAPSIM); //where 'IsNOTdotAPSIM' = bool method(string) - see below

//other code goes here
}

bool IsNOTdotAPSIM(string s)
{
return !s.EndsWith(".apsim");
}




And so today is the day I learnt how to use a predicate! Kind of.

EDIT: @Mental: I mean a hardcoded search using Directory.GetFiles(path, "a search pattern")

This post has been edited by scalt: 21 January 2010 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 Slingsby  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 18-August 09

Re: Directory.GetFiles() Search Pattern

Posted 21 January 2010 - 10:07 PM

What is it exactly you are doing with the files? If you are, say, adding them to a list, you could do something like

  DirectoryInfo dir = new DirectoryInfo("insert directory path");
			foreach(FileInfo file in dir.GetFiles())
			{
				if (file.Extension.Contains("apsim"))
				{
				}
				else
				{
					listBox1.Items.Add(file);
				}
			}


The same concept would work if you were moving or copying the files etc.

  DirectoryInfo dir = new DirectoryInfo("insert directory path");
			foreach(FileInfo file in dir.GetFiles())
			{
				if (file.Extension.Contains("apsim"))
				{
				}
				else
				{
					file.CopyTo("Destination");
				}
			}

Was This Post Helpful? 0
  • +
  • -

#6 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Directory.GetFiles() Search Pattern

Posted 25 January 2010 - 01:27 PM

I am passing the names as an array to another class I have written where they will be processed (on the next line in fact) but yes you have a good point, that is a more elegant way of doing it if I were to add them to a list.

Though personally I would have done:
            foreach (string file in Directory.GetFiles(path))
                if(!file.EndsWith(".apsim")
                    myList.Add(file);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1