Saturday, July 21, 2007

example | C# .NET | Isolating a directory that DOES or DOES NOT contain a particular substring

Intent: This is useful in instances when you have a highly organized archive with different, similarly-named directories in them, e.g. FolderNameRAW and FolderNameTIF

Overview:   Declare a string array using the Directory.GetDirectories() method. Iterate over each string in the array. Use the string.IndexOf method to  isolate the substring of interest. Execute a comparison, with a result of >= 1 meaning that the substring is found, and a comparison of == -1 indicating that the substring was not found. 

 

   string[] dirs = Directory.GetDirectories(sourceImageDirBase);

foreach (string dir in dirs)
{
if (dir.IndexOf("RAW") >= 1)
{
                    // Do something here to images that DO have the string RAW as part of their name
}

{
if (dir.IndexOf("RAW") == -1)
{
                    // Do something here to images that DO NOT have the string RAW as part of their name
                    }

}

}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home