Copy all files from one folder to another in VB 6.0

In this article we will learn little about FileSystemObject using Visual Basic. In Visual Basic, FileSystemObject is available for File system I/O operation.

It is originally created for the Visual Basic Scripting Edition. You can not find FileSystemObject in object library for Visual Basic, actually it is not included in the object library so you need to select the ‘Microsoft Scripting Run-time’ in the Project References dialog box for your project. If you are not able to find this in the list than locate Scrrun.dll on your system.

The following code describes to copy all files from one directory to another directory using FileSystemObject in vb 6.0

This example requires a form with one CommandButton control named ‘Command1’. See the click event handler of command1 in the below

Private Sub Command1_Click()
   CopyFiles "C:\Source", "C:\Destination"
End Sub

The below function contains code to copy all files from one folder to another folder. The function CopyFiles accepts two argument: one for source folder path and another for destination folder path.

Public Function CopyFiles(ByRef strSource As String, ByRef strDestination As String)
        Dim objfso
        Set objfso = CreateObject("Scripting.FileSystemObject")
        Dim strFile As String
        On Error GoTo ErrHandler
        If Right$(strSource, 1) <> "\" Then strSource = strSource & "\"
        If Right$(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
        strFile = Dir(strSource & "*.*")
        Do While Len(strFile)
            With objfso
               If Not .FolderExists(strDestination) Then .CreateFolder (strDestination)
                    .CopyFile strSource & strFile, strDestination & strFile
            End With
            strFile = Dir
        MsgBox "Copying all files successfully"
        Set objfso = Nothing
        Exit Function
        MsgBox "Error in Copying files()" & vbCrLf & " & Err.Description, vbCritical"
End Function

FileSystemObject can be used for others tasks too such as to get the size of specified file or read the text file. The following example demonstrates how you can get the size of the given file.

 Private Sub Command2_Click()
    Dim objfso As New FileSystemObject
    Dim objFile As File
    'Get a reference to the File object.
    Set objFile = objfso.GetFile("d:/mySettings.txt")
    MsgBox objFile.Size
End Sub

See another use of FileSystemObject to read the specified text file:

Private Sub Command1_Click()
    Dim objfso As New FileSystemObject
    Dim objTextStream As TextStream
    'Open file.
    Set ts = objTextStream.OpenTextFile("d:/mySettings.txt")
   'Read file line by line
    Do While Not objTextStream.AtEndOfStream
        Debug.Print objTextStream.ReadLine
    'Close the file.
End Sub

2 thoughts on “Copy all files from one folder to another in VB 6.0”

  1. Can i use above code for copying hidden files also. I have a folder that contains two hidden files and i need to copy these files into another folder…i am using vb6.

  2. if you are using than you can copy hidden files from one to another folder like as:

    Dim myDirectory As DirectoryInfo
    myDirectory = New DirectoryInfo(“C:”)
    Dim aFile As FileInfo
    For Each aFile In myDirectory.GetFiles
    If (aFile.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
    ‘ write the code to copy file
    End If

Comments are closed.