Iterate over filename My program inspects filenames which are delimited via underscores. So an example may be:


  This_is_an_example_of_my_filename.tif


A counter example may be:


  This_is_a_very_bad_document.tif


The filename should always consist of 7 distinct values within the filename. Those values are used to create an instance of a class object which allows me to further validate data based on segment location in the filename. 

The program should only consider *.tif files in the directory, and ignore all others.

If the filename is invalid (too few/too many fields, or field data is improperly formatted) we move it to an error bin. If it is good, I upload the data to a database and move the file on it's merry way.

The code that pulls these filenames apart looks like: 

foreach(string file in Directory.GetFiles(path))
{
    FileInfo fi = new FileInfo(file)
    if(fi.Extension == ".tif")
    {
        MyDocumentObject do = new MyDocumentObject();
        string filename = fi.Name;
        string[] filenameAsArray = filename.split('_');
        string lastIndexNoExtension = (filenameAsArray[filnameAsArray.Length -1].Split('.'))[0];
        filenameAsArray.SetValue(lastIndexNoExtension, (filenameAsArray.Length - 1));  
        if(filenameAsArray.Length == 6)
        {
            for(int x=0;x&lt;=(filenameAsArray.Length - 1);x++)
            {
                switch(x)
                {
                    case 0:
                        string part1 = filenameAsArray[x];
                        do.part1 = part1;
                        break;

                    //so on for 1-6
                }
            }
        }
        bool validationResults = do.ValidateFields();
        if(validationResults)
        {
            File.Move(fi.FullName, "path\to\destination");
        }
        else
        {
            File.Move(fi.FullName, "path\to\errorbin");
        }
    }
}


My main point of focus for this review is on the method of pulling apart the filename into an array of text segments and then assigning that data to an instance object and operating on it. 

In practice I'm pretty happy - it does the job and is zippy about it. But looking at it... just bugs me.

EDIT: I forgot to mention the reason I use my method of splitting to remove the file extension is due to issues that arise when a file in the directory is named something like: 


  this_is_a_trick.txt.tif
First since you only care about tif extensions just pass that into the Directory.GetFiles

foreach (string file in Directory.GetFiles(path, "*.tif"))


No need for an if statement checking extension 

Also you don't need to create a FileInfo object to get the name of the file use 

string filename = Path.GetFileName(file);
filename = filename.Substring(0, filename.IndexOf('.'));


returns you the file name without the extension so you don't need to do the array manipulation.  

If me since there is only 6 values I wouldn't do a loop statement.  You need to code each switch and set each property so the loop isn't really helping you save code nor time.  Just set them.  if they need to change in the future it's less code to change and the loop and switch would need to change as well.  

string filename = Path.GetFileName(file);
filename = filename.Substring(0, filename.IndexOf('.'));
string[] filenameAsArray = filename.Split('_');
if (filenameAsArray.Length == 6)
{
    document.part1 = filenameAsArray[0];
    document.part2 = filenameAsArray[1];
    document.part3 = filenameAsArray[2];
    document.part4 = filenameAsArray[3];
    document.part5 = filenameAsArray[4];
    document.part6 = filenameAsArray[5];
}
bool validationResults = document.ValidateFields();
if (validationResults)
{
    File.Move(file, @"path\to\destination");
}
else
{
    File.Move(file, @"path\to\errorbin");
}


I also changed your object from do to document because do to most English speakers is a verb and they not going to think it stands for document object.  Again for years later whoever looks at this to make it easier to understand.