copy and retain directory structure

murrayf1

Member
Joined
May 5, 2006
Messages
21
Programming Experience
1-3
Ok,

Is there anyway to retain the folder structure as they are on C:\ ?
 
Last edited:
Im not sure what you mean by retain the folder structure. Please be more specific, im a lil lost. Also, thanks John for the filter key inside of the GetFiles command. Wonder if this supports multiple filters b/c I use endswith as a double boolean to search for mroe than one specific file type. I will experiment with this also, I think that filter will increase search time.
 
Ok sorry i should have been more specific

If i have a file C:\test\1.ptf and a file C:\test2\2.ptf they will show up after copying as 1.ptf and 2.ptf in F:\

is there anyway to keep the folders so they become F:\test and F:\test2 while only copying folders that contain ptf files
 
Ok, so what you want is to copy the directory as well. Actually as far as I know with the Directory class there is no way to copy the entire directory. However; there are a few ways around this. I have used this procedure before myself and compiled the process into a dll to use it more often. It contains the search and all inside of it if your interested in purchasing it maybe. But anyways, to teach you how to do it yourself there are a few different ways to accomplish it. Remember these are ways that I have learned and accomplished on my own, there may be a built in feature and or much easier way to do this. The ways I know are as follows.

When you use DirectoryGetFiles copy use the entire string as the new file name. For example, when you use filecopy you are getting that specific file then copying it to whatever directory. Instead, change the first letter "the root of the directory" to the letter of your choice and copy files there. You may have to build a command one step ahead of it to create the directory first on the new drive. Basically from what I am gathering you are trying to clone or ghost all the files of one file type to another drive. That isn't a terribly hard process but it is just a lil tricky. I can try to post some code later but I am at work and my work schedule is now 12 hour shifts. So I have lil time until Sunday-Tuesday to be posting code. Sun-Tues I am available for most of the day. See if what I told you can help u for now.
 
Split thread because discussion went off topic.
 
VB.NET:
Option Explicit On 
Option Strict On

Imports System.IO

Friend Class CopyFiles
    Friend Event Successful()
    Friend Event Failed(ByVal Reason As String)

    Private mstrSource As String
    Private mstrDestination As String

    Friend Sub StartCopying()
        Try
            Call CopyDirectory(mstrSource, mstrDestination)
            RaiseEvent Successful()
        Catch ex As Exception
            RaiseEvent Failed(ex.Message)
        End Try
    End Sub

    Private Function CopyDirectory(ByVal Src As String, ByVal Dest As String) As Boolean
        'add Directory Seperator Character (\) for the string concatenation shown later
        If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then Dest += Path.DirectorySeparatorChar
        'If Directory.Exists(Dest) = False Then Directory.CreateDirectory(Dest)
        Dim Files() As String = Directory.GetFileSystemEntries(Src)
        For Each element As String In Files
            If Directory.Exists(element) = True Then
                'if the current FileSystemEntry is a directory,
                'call this function recursively
                Directory.CreateDirectory(Dest & Path.GetFileName(element))
                CopyDirectory(element, Dest & Path.GetFileName(element))
            Else
                'the current FileSystemEntry is a file so just copy it
                File.Copy(element, Dest & Path.GetFileName(element), True)
            End If
        Next element
    End Function

    Friend Property Source() As String
        Get
            Return mstrSource
        End Get
        Set(ByVal Value As String)
            mstrSource = Value
        End Set
    End Property

    Friend Property Destination() As String
        Get
            Return mstrDestination
        End Get
        Set(ByVal Value As String)
            mstrDestination = Value
        End Set
    End Property
End Class
and to use this:

VB.NET:
Imports System.IO
Imports System.Threading

Private thrdCopyFiles As Thread
Private WithEvents CopyProfile As New CopyFiles

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With CopyProfile
            .Source = "C:\"
            .Destination = "F:\"
        End With
        thrdCopyFiles = New Thread(AddressOf CopyProfile.StartCopying)
        With thrdCopyFiles
            .IsBackground = True
            .Start()
        End With
    End Sub

    Private Sub CopyProfile_Successful() Handles CopyProfile.Successful
        MessageBox.Show("All files have been copied", "Copying Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub CopyProfile_Failed(ByVal Reason As String) Handles CopyProfile.Failed
        MessageBox.Show("Error copying files" & ControlChars.NewLine & Reason, "Error Copying Files", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub
 
Back
Top