macro for moving folders

errtu

Board Regular
Joined
Sep 23, 2010
Messages
134
I have two folders A & B.

I need a macro that will create a new folder C and move all contents from A & B to C.

edit. i forgot, every time it should create a different folder. so that everyday i'll get folder C, D, E, F (maybe name the folders based on date)
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Define the Folders (in red) for Folder A and Folder B
The macro assumes all the files in both folders have unique names.

Code:
Sub Move_Files()
    
    Dim Folders, Folder, FolderC$
    Dim Ext$, MoveFile$, i&
    
    Folders = Array([COLOR="Red"]"C:\Temp1\", "C:\Temp2\"[/COLOR])
    FolderC = "C:\" & Format(Date, "mmm dd yyyy") & "\"
    
    If Len(Dir(FolderC, vbDirectory)) = 0 Then MkDir FolderC
    Ext = "*.xls*"
    
    For Each Folder In Folders
        MoveFile = Dir(Folder & Ext)
        Do While Len(MoveFile)
            Name Folder & MoveFile As FolderC & MoveFile
            i = i + 1
            MoveFile = Dir()
        Loop
    Next Folder
    
    If i Then
        MsgBox i & " files moved to " & FolderC, vbInformation, "Files Moved"
    Else
        MsgBox "No files were moved."
    End If
    
End Sub
 
Upvote 0
Define the Folders (in red) for Folder A and Folder B
The macro assumes all the files in both folders have unique names.

Code:
Sub Move_Files()
    
    Dim Folders, Folder, FolderC$
    Dim Ext$, MoveFile$, i&
    
    Folders = Array([COLOR=Red]"C:\Temp1\", "C:\Temp2\"[/COLOR])
    FolderC = "C:\" & Format(Date, "mmm dd yyyy") & "\"
    
    If Len(Dir(FolderC, vbDirectory)) = 0 Then MkDir FolderC
    Ext = "*.xls*"
    
    For Each Folder In Folders
        MoveFile = Dir(Folder & Ext)
        Do While Len(MoveFile)
            Name Folder & MoveFile As FolderC & MoveFile
            i = i + 1
            MoveFile = Dir()
        Loop
    Next Folder
    
    If i Then
        MsgBox i & " files moved to " & FolderC, vbInformation, "Files Moved"
    Else
        MsgBox "No files were moved."
    End If
    
End Sub

I get user-defined type not defined
 
Upvote 0
Is there a line that is highlighted when you get the error?

Try using this instead as the two Dim lines...

Code:
    Dim Folders As Variant, Folder As Variant, FolderC As String
    Dim Ext As String, MoveFile As String, i As Long

Did you change the text in red to something else? If yes, what?
 
Upvote 0
ok my bad i added a Dim objFSO As FileSystemObject line and forgot to reference windows script host object model.

I get path not found though, and get

Rich (BB code):
Private Sub DONE_Click()
 Dim Folders, Folder, FolderC$
    Dim Ext$, MoveFile$, i&
    
    Folders = Array("C:\Users\Khaos\Documents\My Dropbox\corte electronico\", "C:\Users\Khaos\Documents\My Dropbox\Laur\")
    FolderC = "C:\Users\Khaos\Documents\My Dropbox\Public\LOGS DIARIOS\" & Format(Date, "mmm dd yyyy") & "\"
    
    If Len(Dir(FolderC, vbDirectory)) = 0 Then MkDir FolderC
    Ext = "*.xls*"
    
    For Each Folder In Folders
        MoveFile = Dir(Folder & Ext)

mkdir gets highlighted
 
Upvote 0
The code I posted doesn't use FileSystemObject

MkDir FolderC will make just the last folder (the dated folder) in in the string FolderC
My guess is that the full path C:\Users\Khaos\Documents\My Dropbox\Public\LOGS DIARIOS\ or specifically the folder LOGS DIARIOS\ doesn't exist yet.
 
Upvote 0
I think I'm running into the same issue, as I use scripting, too. I'm having some issues with my code. Any alternatives to get this to work would be greatly appreciated. Here is the code:

Sub Copy_Rename_File1()
Dim FromPath As String, ToPath As String, CheckPath As String
Dim Lastrow As Long, i As Long

Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

If MsgBox("Are you sure you want to move all stale files?", vbYesNo) <> vbYes Then
Exit Sub
End If

If Len(Dir((ActiveSheet.Cells(i, 3).Value & "\Stale Files"), vbDirectory)) = 0 Then
MkDir (ActiveSheet.Cells(i, 3).Value & "\Stale Files")
On Error GoTo 0

End If

i = 1 + i
0 For i = 1 To Lastrow
FromPath = ActiveSheet.Cells(i, 4).Value
ToPath = ActiveSheet.Cells(i, 3).Value & "\Stale Files\" & ActiveSheet.Cells(i, 2).Value

FileCopy Source:=FromPath, Destination:=ToPath

Next i

End Sub
 
Upvote 0
This part of your code...
Code:
If Len(Dir((ActiveSheet.Cells(i, 3).Value & "\Stale Files"), vbDirectory)) = 0 Then
MkDir (ActiveSheet.Cells(i, 3).Value & "\Stale Files")
On Error GoTo 0
End If
...is before the For-Next i loop. So Checking\Making each directory "\Stale Files" path was not within the loop.


Try something like this (not tested)...
Code:
Sub Copy_Rename_File1()
    
    Dim FromPath As String, ToPath As String, i As Long
    
    If MsgBox("Are you sure you want to move all stale files?", vbYesNo) <> vbYes Then Exit Sub
    
    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
        
        If Len(Dir((Cells(i, "C").Value & "\Stale Files"), vbDirectory)) = 0 Then
            MkDir Cells(i, "C").Value & "\Stale Files"
        End If
        
        FromPath = Cells(i, "D").Value
        ToPath = Cells(i, "C").Value & "\Stale Files\" & Cells(i, "B").Value
        FileCopy Source:=FromPath, Destination:=ToPath
        
    Next i
    
End Sub
 
Last edited:
Upvote 0
Alpha Frog,

This seems to be working. I will continue to test it but thanks so much for the quick response and great advice!

I'm an intern for a financial institution and if "alpha" in your name means you're investment-related, best of luck grabbing alpha from your trading.

I appreciate your help!

-Peter
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,876
Members
452,949
Latest member
Dupuhini

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