richard smith

richard smith

  • NA
  • 285
  • 161.1k

Search Directory for Array of filenames

Jan 28 2013 11:29 AM
I have 3 - 5 filenames that will be in either 3 - 4 directory's.  So for example NOF1 = "Finalized_Data" NOF2 = "Final_Data" NOF3 = "Fin_Data" could be in dir1 = "C:\Test\Data\ready\" or dir2 = "C:\Test\Data\"
or dir3 = "C:\Test\Data\Ready Files\" or dir4 = "CL\Test\Data Ready\"

What would be the C# code to search each of those directories for each of the filenames?

If you need to see the larger picture, I want to find the file, open the file, refresh the sql query in the workbook and save it.  I know C# code to open/refresh/save, I just don't know how to search for an array of workbooks in an array of directories.

Answers (2)

0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Jan 28 2013 1:04 PM
Try something like this:

       string dir1 = @"C:\Test\Data\ready\";
       string dir2 = @"C:\Test\Data\";
       string dir3 = @"C:\Test\Data\Ready Files\";
       string dir4 = @"C:\Test\Data Ready\";
       string[] dirPaths = {dir1, dir2, dir3, dir4};
       string NOF1 = "Finalized_Data";
       string NOF2 = "Final_Data";
       string NOF3 = "Fin_Data";
       string[] fileNames = {NOF1, NOF2, NOF3};

       foreach(string fileName in fileNames)
       {
          foreach(string dirPath in dirPaths)
          {
             foreach(string filePath in Directory.GetFiles(dirPath))
             {
                string temp = Path.GetFileName(filePath).ToLower();
                if (temp == fileName.ToLower())
                {
                   // do your stuff
                   break;
                }
             }
          }
       }
Accepted Answer
0
richard smith

richard smith

  • 0
  • 285
  • 161.1k
Jan 28 2013 1:11 PM
Perfect, thank you.