selecting a folder and copying all files into another folder

Holley

Board Regular
Joined
Dec 11, 2019
Messages
155
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello everyone! Hope someone may be able to assist with a project I am working on. I would like to be able to run a macro that will allow me to select a folder and have all the files (they will be .csv) copied into a another folder. The only static address will be the "to" folder. The "from" folder will change each time it is needed.

For example, I would like to move all files in H:\holley\Store\09-05-24\ to H:\holley\Store\Run. It seems like it should be simple, but I'm getting stuck on seleting the initial folder. Any suggestions would be most appreciated! Thank you in advance!
 
Replace it
VBA Code:
initialPath = "C:\Users\Trev\Documents"
on
VBA Code:
initialPath = "H:\holley\Store folder"
its odd, it keeps taking me back to My Documents. I have no reference to C:\ anywhere
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Have you changed anything as I've suggested as well as Serguis who has looked at my code.

Please show the current code your trying to use otherwise you'll not get a solution.
 
Upvote 0
Have you changed anything as I've suggested as well as Serguis who has looked at my code.

Please show the current code your trying to use otherwise you'll not get a solution.
So sorry for the delay in responding, flying solo at work and swampped! The only thing I have changed is the location.. here is the code. I appreciate your help!!

VBA Code:
Option Explicit

Sub MoveToIris2()

    Dim fso As Object
    Dim targetFolder As Object

    Dim fromPath As String
    Dim intialPath As String
    Dim toPath As String

    ' Initialize the file system object
    Set fso = CreateObject("Scripting.FileSystemObject")

    fromPath = "\\abc00012\HOLLEY\Test\PROD\Store\" 'Change this to the folder to select from
    intialPath = "\\abc00012\Collect\Program\Store\" 'Initial Folder Location

    ' Ask user for the destination folder
    Set targetFolder = Application.FileDialog(msoFileDialogFolderPicker)

    ' Define the target folder dialog box properties and result
    With targetFolder
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = intialPath
        If .Show <> -1 Then
            MsgBox "You didn't select anything"
            Exit Sub
        End If
        toPath = .SelectedItems(1)
    End With

    If Right(fromPath, 1) = "\" Then
        fromPath = Left(fromPath, Len(fromPath) - 1)
    End If

    If Right(toPath, 1) = "\" Then
        toPath = Left(toPath, Len(toPath) - 1)
    End If


    If fso.FolderExists(fromPath) = False Then
        MsgBox fromPath & " doesn't exist"
        Exit Sub
    End If

    fso.CopyFolder Source:=fromPath, Destination:=toPath

    MsgBox "You can find the files and subfolders from " & fromPath & " in " & toPath

End Sub
 
Upvote 0
So sorry for the delay in responding, flying solo at work and swampped! The only thing I have changed is the location.. here is the code. I appreciate your help!!

VBA Code:
Option Explicit

Sub MoveToIris2()

    Dim fso As Object
    Dim targetFolder As Object

    Dim fromPath As String
    Dim intialPath As String
    Dim toPath As String

    ' Initialize the file system object
    Set fso = CreateObject("Scripting.FileSystemObject")

    fromPath = "\\abc00012\Collect\Collections\Program\Store\" 'Change this to the folder to select from
    intialPath = "\\abc00012\Collect\Program\Store\" 'Initial Folder Location

    ' Ask user for the destination folder
    Set targetFolder = Application.FileDialog(msoFileDialogFolderPicker)

    ' Define the target folder dialog box properties and result
    With targetFolder
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = intialPath
        If .Show <> -1 Then
            MsgBox "You didn't select anything"
            Exit Sub
        End If
        toPath = .SelectedItems(1)
    End With

    If Right(fromPath, 1) = "\" Then
        fromPath = Left(fromPath, Len(fromPath) - 1)
    End If

    If Right(toPath, 1) = "\" Then
        toPath = Left(toPath, Len(toPath) - 1)
    End If


    If fso.FolderExists(fromPath) = False Then
        MsgBox fromPath & " doesn't exist"
        Exit Sub
    End If

    fso.CopyFolder Source:=fromPath, Destination:=toPath

    MsgBox "You can find the files and subfolders from " & fromPath & " in " & toPath

End Sub
Found out the culpit! Another folder had been added making my path off (corrected above). BUT, now that is corrected, I have ran into a new bump. When I select the folder (its dated with todays date) to copy from \abc00012\Collect\Collections\Program\Store and select, it begins to paste everything from the folder into the folder selected..
 
Upvote 0
VBA Code:
Sub jec()
   Shell "cmd /c copy C:\basefolder\*.* C:\NewFolder\", 0
   Kill "C:\basefolder\*.*"
End Sub
 
Upvote 0
In the vba editor
Thanks, I understand that, would the above need to be added to the existing code? If so, where in there should it go? It looks as if is a seperate macro. Not sure how to work with the above.
 
Upvote 0
This is just an illustration of how you could mass move a folder to another folder. You don’t need the loop.

But you have to change the filepaths to yours.

It is indeed a new macro
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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