HOW TO SIMPLY COPY A DIRECTORY AND ALL IT'S CONTENTS

HOW TO SIMPLY COPY A DIRECTORY AND ALL IT'S CONTENTS

Page 1 of 1

2 Replies - 15763 Views - Last Post: 05 February 2010 - 05:40 AM Rate Topic: -----

#1 peter100191  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 05-May 09

HOW TO SIMPLY COPY A DIRECTORY AND ALL IT'S CONTENTS

Posted 04 February 2010 - 09:31 PM

Hello,

I have been trying for hours to copy a directory and all it's subfolders and files to another location. There must be an easier way to do this as I have been looping through all the directory's subfolders trying to parse the address of the directory to copy it to a new location in the same sub folder. To prove I have been working on it, here is some unfinished code, it's absolute garbage and does not even work.

Dim x As Integer
        While x <> globdir.Length
            For Each f As FileInfo In globdir(x).GetFiles()
                Console.WriteLine("COPYING " & f.Name & "...")
                Try
                    'File.Copy(par(0) & "\" & f.Name, par(1) & "\" & famdir.Name & "\" & f.Name)
                    Console.WriteLine("DONE!")
                Catch
                    Console.WriteLine("FAILED!")
                End Try
            Next
            Console.WriteLine("COPYING " & globdir(x).Name & "...")
            Try
                Dim splt() As String = globdir(x).FullName.Split(":")
                Dim splt2() As String = par(1).Split(":")
                Directory.CreateDirectory(par(1) & splt(1))
                Console.WriteLine("DONE!")
            Catch
                Console.WriteLine("FAILED!")
            End Try
            For Each Dir As DirectoryInfo In globdir(x).GetDirectories
                globdir(globdir.Length + 1) = Dir
            Next
            x += 1
        End While
        'For Each d As DirectoryInfo In famdir.GetDirectories
        'Next
        inputcheck()
    End Sub



That is a headache half finished sub that I will finish if there is no other way... Help? :)

This post has been edited by peter100191: 04 February 2010 - 09:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: HOW TO SIMPLY COPY A DIRECTORY AND ALL IT'S CONTENTS

#2 r1zla  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 142
  • Joined: 04-February 10

Re: HOW TO SIMPLY COPY A DIRECTORY AND ALL IT'S CONTENTS

Posted 05 February 2010 - 12:16 AM

Hello,

A quick google search for "copy directory vb.net" led me to this site.

Below is a quote of what it says:

Quote

''Use the FindFirstFile, FindNextFile, and FindClose API functions. Use MkDir to create directories and FileCopy to copy files.
 

' Copy all files below this directory. Return the
' number of files copied.
Private Function CopyFiles(ByVal from_dir As String, ByVal _
    to_dir As String) As Long
Dim files_copied As Long
Dim dirs As Collection
Dim fname As String
Dim search_handle As Long
Dim file_data As WIN32_FIND_DATA
Dim i As Integer

    Set dirs = New Collection

    ' Get the first file.
    search_handle = FindFirstFile( _
        from_dir & "*.*", file_data)
    If search_handle <> INVALID_HANDLE_VALUE Then
        ' Get the rest of the files.
        Do
            ' Get the file name.
            fname = file_data.cFileName
            fname = Left$(fname, InStr(fname, Chr$(0)) - 1)

            ' Skip the files "." and "..".
            If fname <> "." And fname <> ".." Then
                files_copied = files_copied + 1

                ' See if the file is a directory.
                If file_data.dwFileAttributes And _
                    DDL_DIRECTORY Then
                    ' This is a directory.
                    ' Make the new directory.
                    MkDir to_dir & fname

                    ' Save the directory name so
                    ' we can search it later.
                    dirs.Add fname
                Else
                    ' This is not a directory.
                    ' Copy the file.
                    FileCopy from_dir & fname, to_dir & _
                        fname
                End If
            End If

            ' Get the next file.
            If FindNextFile(search_handle, file_data) = 0 _
                Then Exit Do
        Loop

        ' Close the file search hanlde.
        FindClose search_handle
    End If

    ' Search subdirectories.
    For i = 1 To dirs.Count
        fname = dirs(i)
        files_copied = files_copied + CopyFiles(from_dir & _
            fname & "\", to_dir & fname & "\")
    Next i

    CopyFiles = files_copied
End Function

' Copy from_file to to_file. If from_file is a
' directory, copy it and its files, creating to_file
' if necessary.
Private Function XCopyFile(ByVal from_file As String, ByVal _
    to_file As String) As Long
Dim files_copied As Long

    ' See if from_file is a directory.
    If GetAttr(from_file) And vbDirectory Then
        ' This is a directory.
        If Right$(from_file, 1) <> "\" Then from_file = _
            from_file & "\"
        If Right$(to_file, 1) <> "\" Then to_file = to_file _
            & "\"

        ' Create to_file if necessary.
        On Error Resume Next
        MkDir to_file
        If Err.Number = 0 Then files_copied = 1
        On Error GoTo 0

        ' Copy the files in the directory.
        files_copied = files_copied + CopyFiles(from_file, _
            to_file)
    Else
        ' This is not a directory.
        ' Copy the file.
        FileCopy from_file, to_file

        files_copied = 1
    End If

    XCopyFile = files_copied
End Function


Hope this helps.

Edit: Or I suppose you could use the shell extension and run xcopy with the correct extensions.

This post has been edited by r1zla: 05 February 2010 - 12:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 nmgod  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: HOW TO SIMPLY COPY A DIRECTORY AND ALL IT'S CONTENTS

Posted 05 February 2010 - 05:40 AM

    Private Sub CopyDirectory(ByVal sOriginal As String, ByVal sDestination As String)
        Dim oFiles() As IO.FileInfo
        Dim oFile As IO.FileInfo
        Dim oDirectory As New IO.DirectoryInfo(sOriginal)
        IO.Directory.CreateDirectory(sDestination)

        Try
            oFiles = oDirectory.GetFiles()
        Catch ex As UnauthorizedAccessException
            Exit Sub
        End Try

        For Each oFile In oFiles
            IO.File.Copy(oFile.FullName, sDestination & "\" & oFile.FullName.Substring(sOriginal.Length))
        Next
 
        Try
            For Each oEntry As IO.DirectoryInfo In oDirectory.GetDirectories
                CopyDirectory(oEntry.FullName, sDestination & "\" & oEntry.FullName.Substring(sOriginal.Length))
            Next
        Catch ex As Exception
        End Try
    End Sub


Implementation
        CopyDirectory("C:\Users\Tank\Documents\My Received Files", "C:\Users\Tank\Desktop\My Received Files")


This will also make sure that it won't crash if the user doesn't have access to a sub-folder
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1