1

What I am trying to do is search through a folder's subdirectories and any folders that have /Year/Month/ I want to pull the files from that folder.

The folder paths are typically as follows

BaseFilePath\Group1\SubGroup1\Year\Month BaseFilePath\Group1\Year\Month

The goal is to move those files into another folder and while I have tried iterating through the whole directory it just seems to take too long, and I was wondering if there was a quicker or easier solution.

The code I was using to do this is as follows

    Dim topLevelFolder As New DirectoryInfo("\\BaseFilePath\")

    Using outputFile As New StreamWriter("C:\output_file.txt")
        For Each currentFile In topLevelFolder.EnumerateFiles("*.*", SearchOption.AllDirectories)
            Try
                If currentFile.Directory.Name = "June" AndAlso currentFile.Directory.Parent.Name = "2014" AndAlso Left(currentFile.Name, 5) <> "SENT_" Then
                    outputFile.WriteLine(currentFile.Directory.Parent.Parent.Name & "/" & currentFile.Directory.Parent.Name & "/" & currentFile.Directory.Name & "/" & currentFile.Name)
                End If
            Catch
            End Try

        Next
    End Using

This portion alone is already taking close to 30 seconds, and I'd imagine moving the files to another folder is going to take some time too, but if I can speed up the finding of the files the whole process would go smoother.

I saw that you can use wildcards in the searchPattern, but when I tried I got illegal characters in path or Second path fragment must not be a drive or UNC name. I know individual wildcards are useable ie (BaseFilePath\partialFolderName*) but doesn't seem to help when it extends to multiple directories being wildcarded.

Ideally what I'm looking to do is search BaseFilePath*2014\June* and only pull files from those folders.

Any help or suggestions would be greatly appreciated

Jerry
  • 53
  • 1
  • 6

1 Answers1

1

You can do something like this:

topLevelFolder.EnumerateDirectories("*2014", SearchOption.AllDirectories)

Then out of those, filter June ones. Then enumerate files in each of the result entries.

Unfortunately, .NET does not support directory wildcard search for the full path, so this does not work *2014\June*, it complains about invalid characters.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • topLevelFolder.EnumerateDirectories("*2014", SearchOption.AllDirectories) seems to still take up a bulk of time. About 15-20 seconds on average for that line alone, the rest of the code below finishes within a second. – Jerry Jul 30 '14 at 19:01
  • @Jerry: You can look into getting an SSD, or start using async. Check this out - http://stackoverflow.com/questions/719020/is-there-an-async-version-of-directoryinfo-getfiles-directory-getdirectories-i – Victor Zakharov Jul 30 '14 at 19:36