Simplest Way to Transfer 'All' Files, From One Folder to Another

epoiezam

New Member
Joined
Jan 28, 2016
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

Is there a simplest way to just transfer multiple files from one folder to another.
Prob is, file name can change, so I can't fix it in the prog.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The title of the thread says "all" files, and your query asks about "multiple" files. It doesn't matter that the filenames will change, but if you're only copying across multiple files, and not all, what instructions will you be giving to VBA as to which files are to be copied? Also, is the intention to move them or just copy them?
 
Upvote 0
Hey Dan..
Sorry for the mixup.. Lets go back to the title.. All files in a folder.. :)
 
Upvote 0
@epoiezam, you said file name can change, did you mean to say folder name? Either way, how are we to decide which one to use if it changes?
 
Upvote 0
Hey Dan..
Sorry for the mixup.. Lets go back to the title.. All files in a folder.. :)
This will move all files from one folder to another, unless the file already exists in the destination folder. It also tests to make sure that both folders exist, etc. otherwise you'll have errors to deal with. Is that kinda what you wanted?

VBA Code:
Sub TestSubroutine()
    
    MoveFiles "D:\FromThisFolder\", "D:\OverToThisFolder"
    
End Sub

Sub MoveFiles(ByVal SourceDirectory As String, ByVal DestinationDirectory As String)
    
    Dim FSO             As Object
    Dim TargetFile      As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    If Right(SourceDirectory, 1) <> "\" Then SourceDirectory = SourceDirectory & "\"
    If Right(DestinationDirectory, 1) <> "\" Then DestinationDirectory = DestinationDirectory & "\"
    
    If FSO.FolderExists(SourceDirectory) And FSO.FolderExists(DestinationDirectory) Then
        For Each TargetFile In FSO.getfolder(SourceDirectory).Files
            If FSO.FileExists(DestinationDirectory & TargetFile.Name) = False Then
                TargetFile.Move DestinationDirectory
            End If
        Next
    Else
        MsgBox "Error - the path details are incorrect"
        Exit Sub
    End If
    
    Set FSO = Nothing
    Set TargetFile = Nothing
End Sub
 
Upvote 0
Solution
Hey Dan, it's right on the dot.. Just what I need.. Truly appreciate it. ;)
Not forgetting to the rest of you guys, Thank you.
 
Upvote 0
Thank you for the feedback. Glad it works. (phew!)
 
Upvote 0

Forum statistics

Threads
1,215,537
Messages
6,125,386
Members
449,221
Latest member
DFCarter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top