VB code to transfer files from a folder to another folder

Posted on 2005-04-25
Last Modified: 2013-11-13
hi guys! i need help!
i need a code that will transfer all the files in one folder to another folder.

folder c:\mainfolder contains picture files, and every now and then new pictures will be added to that directory.
its like saying folder mainfolderis the main repository of the picture files.

lets say i have another folder named temp. what i want the code to do is copy all the new files added to the mainfolder or all the files in it and copy it to temp folder overwriting the existing ones. i dont want windows to ask me if i want to overwrite the existing files, i want the code to just copy all the files from the mainfolder.

thanks in advance for the help!
have a nice day!

Question by:c0zee
    LVL 19

    Accepted Solution

    Hi there, if can use shfileoperation

    Option Explicit
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    Const FO_COPY = &H2
    Const FO_DELETE = &H3
    Const FO_MOVE = &H1
    Const FO_RENAME = &H4
    Const FOF_ALLOWUNDO = &H40
    Const FOF_SILENT = &H4
    Const FOF_FILESONLY = &H80

       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 Sub cmdcopy_Click()

    Dim result As Long, filecopy As SHFILEOPSTRUCT

    With filecopy
           .hwnd = Me.hwnd
           .wFunc = FO_COPY
           .pFrom = "c:\mainfolder\*" & vbNullChar & vbNullChar
           .pTo = "c:\otherfolder" & vbNullChar & vbNullChar
           .fFlags = FOF_NOCONFIRMMKDIR
    End With
    result = SHFileOperation(filecopy)
    If result <> 0 Then
           ' Operation failed
           MsgBox Err.LastDllError
    End If

    End Sub
    LVL 12

    Assisted Solution

    A simpler way (don't know about which is faster performance wise)

        Dim fso As Scripting.FileSystemObject
        Dim oFolder As Folder
        Dim oFile As File
        Set fso = New FileSystemObject
        Set oFolder = fso.GetFolder("C:\mainfolder")

        For Each oFile In oFolder.Files
            fso.CopyFile oFile.Path, "C:\temp\" & oFile.Name
            oFile.Delete True
        Next oFile
        Set fso = Nothing
        Set oFolder = Nothing
        Set oFile = Nothing

    Author Comment

    both of them worked fine!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now