0

First time poster here so please be gentle.

I’m using Visual Studio Express 2010

I’m trying to create a backup program to backup specific folders and files each night to an external hard drive.

I’m using:

Dim dirs As List(Of String) = New List(Of String)(Directory.EnumerateDirectories(dirPath))

To get a list of directories on my local drive and then:

For Each folder In dirs

To cycle through all the directories and then:

    If Not Directory.Exists(FdriveDirName) Then
    My.Computer.FileSystem.CopyDirectory(CdriveDirName, FdriveDirName, True)
    End if

This works fine for the first copy. My problem is when I create a new folder within a folder in the root directory (for example installing a new program would create a new folder in program files in c:) This new folder is NOT copied the 2nd time I run the program.

I can see the reason behind this is that the EnumerateDirectories function only lists folders one level down in the folder hierarchy.

I clearly need to list all folders AND subfolders but even replacing EnumerateDirectories with getdirectories hasn’t helped me.

Any one with any ideas?

2 Answers2

0

You need to walk the tree. You need to create a recursive function iterates through the sub-directories of directories. For example

Here is the crux of it:

Private Sub RecurseDirectories(ByVal di As DirectoryInfo)
    Try
        For Each d In di.GetDirectories()
            'Do stuff with the d (directory) here.
            RecurseDirectories(d) 'get sub directories of this directory
        Next
    Catch
    End Try
End Sub
Community
  • 1
  • 1
Donal
  • 31,121
  • 10
  • 63
  • 72
0

This will probably get you into the realm of a recursive algorithm that searches "Depth First"

So, you would create a function that you can call recursively on every folder before it processes the folders at the current level:

Public Sub ProcessFolders(ByVal inDirectoryPath As String)
    For Each subFolderPath In System.IO.Directory.GetDirectories(inDirectoryPath)
        ProcessFolders(subFolderPath)

        'build FdriveDirName

        If Not System.IO.Directory.Exists(FdriveDirName) Then
            My.Computer.FileSystem.CopyDirectory(subFolderPath, FdriveDirName, True)
        End If
    Next
End Sub
rory.ap
  • 34,009
  • 10
  • 83
  • 174