3 Replies - 1045 Views - Last Post: 19 September 2011 - 06:53 AM Rate Topic: -----

#1 Jezzabeanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 11

Recursive copying of directories and contents

Posted 19 September 2011 - 04:32 AM

Hi,

I am currently creating a little application which will essentially backup my work I do at college.

It works by me saving all my work onto a flash drive. When I run the application it will copy all the contents of the flash drive and copying to my college directory and when I'm at home copy to my Dropbox folder. This essentially gives me 4+ areas where my work is saved. Unfortunately, the college computers due to network restrictions and other problems, the use Dropbox on the college machines (I obviously can still access the website).

My problem being that there is no option to copy a directory and its contents to another area as the System.IO.Directory does not have a copy command.

I am currently able to copy files but it isn't recursive and I want the folder to be copied too.

Here's what I currently have:

Imports System.IO
Imports System


Public Class Form1
    Dim FilePath As String
    Dim DestinationPath As String


    Private Sub cmdSync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSync.Click

        FilePath = "J:\Test\"
        If (Directory.Exists(FilePath)) Then
            For Each fName As String In Directory.GetFiles(FilePath)
                If File.Exists(fName) Then
                    Dim dFile As String = String.Empty
                    dFile = Path.GetFileName(fName)
                    Dim dFilePath As String = String.Empty
                    dFilePath = DestinationPath + dFile
                    File.Copy(fName, dFilePath, True)
                End If
            Next
        End If
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            DestinationPath = "W:\"
        Else
           DestinationPath = ""
        End If

        Label1.Text = DestinationPath
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked = True Then
            DestinationPath = "C:\Dropbox\"
        Else
            DestinationPath = ""
        End If

        Label1.Text = DestinationPath
    End Sub
End Class



Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Recursive copying of directories and contents

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Recursive copying of directories and contents

Posted 19 September 2011 - 05:19 AM

I like the DirectoryInfo class. Basic design:

Private Sub BackUpFiles(dir As String, destinationDir As String)
   Dim dirToCheck As New DirectoryInfo(dir)
   'create backup directory 
   For Each fi As FileInfo In dirToCheck.GetFiles()
     'copy files to backup directory
   Next
   For Each dirBelow As DirectoryInfo In dirToCheck.GetDirectories()
     'create the backup directory and pass to sub
     BackUpFiles(dirBelow.Fullname, dest)
   Next
End Sub

This post has been edited by hawkvalley1: 19 September 2011 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Jezzabeanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 11

Re: Recursive copying of directories and contents

Posted 19 September 2011 - 05:29 AM

Thank you for your quick reply. I will try it out when I'm out of my Networking class and get back to you.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Recursive copying of directories and contents

Posted 19 September 2011 - 06:53 AM

I am also thinking of a third parameter for concatenating the backup directory name so it builds correctly. I am in class all day but will check in when I can.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1