It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Copy directory to My Documents subdirectory.

Moderator: dafunkymunky

Copy directory to My Documents subdirectory.

Postby WebAtom on Thu Dec 31, 2009 2:05 am

Hi
I am writing a program that needs to be able to copy a directory from C:\Folder to a folder inside My Documents.

I am aware of the My.Computer.FileSystem.SpecialDirectories.MyDocuments which places the directory in the root of My Documents. I however, want to copy the said folder to 'My Document\ExampleFolder\Folder1'


Thankyou for your help and I apologize if this has been answered elsewhere - I did search but stuff is always missed.
WebAtom
 
Posts: 1
Joined: Thu Dec 31, 2009 2:02 am

Re: Copy directory to My Documents subdirectory.

Postby Tanzim on Thu Dec 31, 2009 9:37 am

Try this, I fou nd it on the internet. :D

Code: Select all
    Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
        Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
        Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)

        ' the source directory must exist, otherwise throw an exception
        If SourceDir.Exists Then
            ' if destination SubDir's parent SubDir does not exist throw an exception
            If Not DestDir.Parent.Exists Then
                Throw New DirectoryNotFoundException _
                    ("Destination directory does not exist: " + DestDir.Parent.FullName)
            End If

            If Not DestDir.Exists Then
                DestDir.Create()
            End If

            ' copy all the files of the current directory
            Dim ChildFile As FileInfo
            For Each ChildFile In SourceDir.GetFiles()
                If Overwrite Then
                    ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True)
                Else
                    ' if Overwrite = false, copy the file only if it does not exist
                    ' this is done to avoid an IOException if a file already exists
                    ' this way the other files can be copied anyway...
                    If Not File.Exists(Path.Combine(DestDir.FullName, ChildFile.Name)) Then
                        ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), False)
                    End If
                End If
            Next

            ' copy all the sub-directories by recursively calling this same routine
            Dim SubDir As DirectoryInfo
            For Each SubDir In SourceDir.GetDirectories()
                CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, _
                    SubDir.Name), Overwrite)
            Next
        Else
            Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
        End If
Tanzim
 
Posts: 11
Joined: Sun Dec 27, 2009 11:22 am


Who is online

Users browsing this forum: No registered users and 3 guests