1

This is a lot, I know; trying to dig myself out of a hole at work.

Basically, I have many files across multiple servers that I need to get a hold of. Right now I'm running in to two problems, 1) I can't figure out the best way to search through multiple UNC paths. 2) I'm having to search by a partial name, it's possible that there is more than one file that matches, but I only want to use the file created in the last three days.

Here is my code so far. I'm not looking for someone to write it, but I would appreciate any logistical pointers.

        uncPath1 = "\\server\share\";
        string partial = "2002265467";
        DateTime date = Convert.ToDateTime("10/5/2015");

        DirectoryInfo a = new DirectoryInfo(uncPath1);
        FileInfo[] interactionlist = a.GetFiles("*" + partial + "*.*", SearchOption.AllDirectories);

        foreach (FileInfo f in interactionlist)
        {
            string fullname = f.FullName;

            Console.WriteLine(fullname);
            Console.Read();
        }
jPol34
  • 61
  • 7

2 Answers2

1

You mentioned that you need to find only files made in the past 3 days. Instead of using Convert.ToDateTime and hard-coding the date in, you should use DateTime.Today.AddDays( -3 ) to get the date three days before the day the program is being run.

And of course, in your finding files method, compare the dates with something like:

DateTime time = DateTime.Today.AddDays( -3 );
if ( File.GetCreationTime( filePath ) > time ) {
    // Add the file
}
Joe
  • 70
  • 8
0

1) You want to make a basic function that looks for a filespec in a single folder. You already wrote that in your code above, you just need to turn it into a function with parameters UNC path and filespec. Have the function take a third parameter of List<FileInfo> to add found files to.

2) If you need to search subfolders, create a function that will search a UNC path's subfolders by calling the function you wrote in #1, then getting a list of all folders, and calling itself for each folder found (and in turn, those calls will call for sub-subfolders, etc.) This is called recursion. Have this function take a List and add all found files to the List, by passing it to your #1 function.

3) Get the root UNC paths you want to search into a List or Array and then call foreach on them passing them, the filespec, and the intially empty List to the #2 function.

So:

bool FindFiles(string uncPath, string fileSpec, List<FileInfo> found);
bool FildFilesSubfolders(string uncPath, string fileSpec, List<FileInfo> found);

string fileSpec = "whatever";
string[] uncPaths = { "abc", "def" };  // etc
List<FileInfo> found = new List<FileInfo>();
foreach (string nextPath in uncPaths)
{
    if (FindFilesSubfolders(nextPath, fileSpec, found))
        break;
}

foreach (FileInfo f in found)
{
    string fullname = f.FullName;
    Console.WriteLine(fullname);
    Console.Read();
}

One final thought: if you are searching subdirs and you are worried about two UNC paths that are essentially duplicates (e.g., c:\foo and c:\foo\foo2), you can use This method to check for paths within another path.

Edit: If you find something you are looking for and want to exit early, have the functions return a boolean meaning you found what you wanted to stop early. Then use break in your loops. I've edited the code.

Community
  • 1
  • 1
Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62
  • Hi Ed, Thank you so much for you help, sorry about not responding last night, I went offline for sanity's sake. I did try to upvote the responses, but it says I can't until I have gained 15 reputation. I've implemented your suggestions, and..it worked! Thank you so much. I was wondering if you you could help one more time maybe? If a file is found, what's the best way to 'quit' searching through the uncPaths? I have four, and it is time consuming to search through all of them. – jPol34 Oct 09 '15 at 15:58
  • @jPol34, use a return code from your find functions and break out of your loops with a break on a true return (or in your original function, when anything is found). I've made an edit above. – Ed Bayiates Oct 09 '15 at 18:16