• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Copy all Files and Folders in Visual Basic

Hi,

  I'm trying to copy all files and folders in one directory to another directory.  The problem is that "CopyFolder" also copies the original directory.  I need to have all the files and folders to be copied to the root of the other directory.  For instance, I need to copy everything in C:\MyStuff to the root of E:\ (without the MyStuff folder).

  I have the following code:
            Dim MyObj 'As New FileSystemObject
            Set MyObj = CreateObject("Scripting.FileSystemObject")
           
            If MyObj.FolderExists(startpath) Then
                MyObj.CopyFolder startpath, destpath
            End If

But as I said, this method copies the "MyStuff" folder also, and not just the contents.

Any ideas?

Thank you,
TK
0
tkeeler1
Asked:
tkeeler1
  • 2
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The DOS command xcopy will copy a folder and all its contents

shell("xcopy c:\1 c:\2")

Copies everything from folder 1 into folder 2.

You may need to set some command line switches to handle sertain scenarios.

Hope this helps.
-Jim
0
 
tkeeler1Author Commented:
I can't use shell commands for my app.  Can you do the same thing with a FileSystemObject?

Thanks,
TK
0
 
iHadiCommented:
Hi tkeeler1

Add a command button to a form and add a reference to microsoft scripting runtime in your project and past the following code:


Dim SrcPath As String
Dim DestPath As String

Private Sub CopyRootFiles()
Dim FSO As New Scripting.FileSystemObject
Dim Fld As Scripting.Folder
Dim tmpFile As Scripting.File

Set Fld = FSO.GetFolder(SrcPath)

For Each tmpFile In Fld.Files
    tmpFPath = Replace(tmpFile.Path, SrcPath, DestPath)
    If FSO.FileExists(tmpFPath) = True Then
        Set tmpCFile = FSO.GetFile(tmpFPath)
        If tmpFile.DateLastModified <> tmpCFile.DateLastModified Then
            tmpFile.Copy tmpFPath, True
        End If
    Else
        tmpFile.Copy tmpFPath, True
    End If
Next
End Sub

Private Function CopyNestedFiles(ByVal Path As String)
Dim FSO As New Scripting.FileSystemObject
Dim Fld As Scripting.Folder
Dim tmpFld As Scripting.Folder
Dim tmpFile As Scripting.File
Dim tmpDPath As String
Dim tmpFPath As String
Dim tmpCFile As Scripting.File

'On Error Resume Next
If Right(Path, 1) <> "\" Then Path = Path & "\"
Set Fld = FSO.GetFolder(Path)

For Each tmpFld In Fld.SubFolders
    DoEvents
    tmpDPath = Replace(tmpFld.Path, SrcPath, DestPath)
    If FSO.FolderExists(tmpDPath) = False Then
        MkDir tmpDPath
    End If
    CopyNestedFiles tmpFld.Path
    ' copy files
    For Each tmpFile In tmpFld.Files
        DoEvents
        tmpFPath = Replace(tmpFile.Path, SrcPath, DestPath)
            '** Uncomment the following if you want the function not to replace newer files
            'If FSO.FileExists(tmpFPath) = True Then
                'Set tmpCFile = FSO.GetFile(tmpFPath)
                'If tmpFile.DateLastModified <> tmpCFile.DateLastModified Then
                    'tmpFile.Copy tmpFPath, True
                'End If
            'Else
                tmpFile.Copy tmpFPath, True
            'End If
    Next
Next
End Function
Private Sub Command2_Click()
SrcPath = "C:\a" ' change to your source folder
DestPath = "C:\b" ' change to your dest folder

CopyRootFiles
CopyNestedFiles SrcPath

MsgBox "Finished"
End Sub
0
 
tkeeler1Author Commented:
Great!  This works.  Thanks a bunch iHadi.....

-TK
0
 
iHadiCommented:
Your welcome tkeeler1
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now