file directory structure change

jassie

Well-known member
Joined
Nov 13, 2012
Messages
61
Programming Experience
1-3
In a C# 2008 desktop application I would like to know how to change the following code so that the files I am looking for can be located.
                 string Format_Date = DateTime.Now.AddMonths(-1).ToString("MM-yyyy");
                     String filesaveLocation = null;
                     filesaveLocation = Path.Combine(ConfigurationSettings.AppSettings["tLocation"], Format_Date);
                     if (!Directory.Exists(filesaveLocation))
                     {
                         System.IO.Directory.CreateDirectory(filesaveLocation);
                         logging.Error("The location " + filesaveLocation + " does not exist.");
                         return packageId;
                     }
 
                    else
                     {
                        string[] RFiles = (from path in Directory.GetFiles(filesaveLocation)
                                              let name = Path.GetFileName(path)
                                              where name.EndsWith(".pdf") ||
                                              name.EndsWith(".xlsx") ||
                                              name.EndsWith(".xls")
                                              select path).ToArray();
                        
                      }
                         foreach (String RFile in RFiles)
                       {
                       }

The code was written for a directory path that looks like the following:
C:\Trans\01-2013 -Note the 01-2013 is for month and year.

Now I am finding out that the file directory stucture in production looks like the following:
R:\Trans\_JAN_2013\A-Sample. Under the A-Sample directory there are 4 separate subdirectories
called: SUB1, SUB2, SUB3, and SUB4.

I was expecting to see the files in one directory instead of 4 separate directories.

Thus can you show me code and/or tell me how you would change the code to accomodae the change in the file directory strucutre?
 
Last edited by a moderator:
The Directory.GetFiles method allows you to specify whether to look in just the specified folder or subfolders also. If that's not specific enough then just write a method that takes a folder and returns the files and then call that method multiple times for the multiple folders, adding each file list to a larger list.
 
Back
Top Bottom