Hi Can anybody explain, How to copy folder in VB6. All the files in that folder should be copied. Please help

Try this code, it needs a form and a button(Command1) -

Option Explicit

Private Declare Function SHFileOperation _
    Lib "shell32.dll" _
        Alias "SHFileOperationA" _
            (lpFileOp As SHFILEOPSTRUCT) As Long

Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Private Const FO_COPY = &H2

Private Sub Command1_Click()

    If CopyFolder("From", "To") Then
        MsgBox "Copied"
    Else
        MsgBox "Not copied"
    End If

End Sub

Private Function CopyFolder(ByVal sFrom As String, _
                            ByVal sTo As String) As Boolean
    Dim SHFileOp As SHFILEOPSTRUCT

    On Error GoTo CF_Err

    CopyFolder = False

    With SHFileOp
        .wFunc = FO_COPY
        .pFrom = sFrom
        .pTo = sTo
    End With
    
    SHFileOperation SHFileOp
    CopyFolder = True

    Exit Function

CF_Err:
    MsgBox "Following error occurd while copying folder " & sFrom & vbCrLf & _
            Err.Description, vbExclamation, "Error message"
End Function

Try:

Private Sub cmdBackup_Click()

call FCopy

End Sub


Public Sub FCopy()

On Error GoTo err:

'myFile - Change if desired

    Dim myFile As New FileSystemObject
 

    Set myFile = Nothing
            
     myFile.CopyFile App.Path & "\MyFolderFrom", App.Path & "\MyfolderTO\", True

'App.Path - if the folder you would like to copy is in the same path as your app
'Change according to your app
'MyFolderFrom - Source, can be the path of the folder to be copied
'MyfolderTO - Destination, the path where the folder will be copied to
'True - Condition if it will overwrite a current folder (with the same name)
'can be False
        
    MsgBox "Folder Copy Success.", vbInformation, "Information"

    
    Exit Sub
err:
    MsgBox err.Description, vbCritical
End Sub

Nice and short ABE, obviously from your database backup app.;)

@Daniwaber, Abelingaw has been through a lot on file copy and replace functions. Follow his lead....:)

On line 20, my bad.

myFile.CopyFile App.Path & "\MyFolderFrom", App.Path & "\MyfolderTO\", True

Replace it to

myFile.CopyFolder App.Path & "\MyFolderFrom", App.Path & "\MyfolderTO\", True

@Sir Andre

Keeping successfully created progs kinda good thing to do :cool:

This article has been dead for over six months. Start a new discussion instead.