Macro to do backup..

help_questions

Board Regular
Joined
Aug 22, 2005
Messages
215
I want to write a macro that will copy the contents of my C:\Test folder and create a folder in C:\Reports (in the mmmm d, yyyy format) and then paste the copied contents of C:\Test into the new folder....

Can this be done with a Macro? if so how??
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Write a batch file that builds the folder you want and copies the files you want to it. Then use the Shell command in your macro to run the batch file.
Code:
Shell "C:\WINDOWS\system32\cmd.exe" & " C:\Mybatch.BAT"
 
Upvote 0
i dont know how to do this in batch. I can mkdir the folder, and copy to the folder...but i dont know how to write a batch file that will make a folder titled with the current date and then copy to this dated folder.....can you help with this?
 
Upvote 0
Sorry, Have to run now. Will check this post tomorrow. Maybe someone can answer before then.
 
Upvote 0
This code worked for me. Didn't need to use a Batch file after all.
Could not use your desired Date format as it contains characters not allowed in folder names.
Code:
Sub Copy2NewFolder()
Dim FSO As Object, f As Object, Path As String
Dim myDir As String
    myDir = "C:\Test"
    NewFolder = "C:\Reports\"
    NewFolder2 = "C:\Reports\" & Format(Now(), "mm-dd-yy") & "\"
    ChDir myDir
    Path = myDir
    
Set FSO = CreateObject("Scripting.FileSystemObject")

'if folder does not exist then create it
    If Dir(NewFolder, vbDirectory) = "" Then
        MkDir NewFolder
    End If
'Create subFolder    
    If Dir(NewFolder2, vbDirectory) = "" Then
        MkDir NewFolder2
    End If

'Copy files
    For Each f In FSO.GetFolder(Path).Files
        FileCopy f, NewFolder2 & f.Name
    Next

End Sub
You will have to setup a reference to Microsoft Objects in VBA to use the FSO code.
Hope this is what you are after.
 
Upvote 0
Glad it worked for you.
Thanks for the follow up, always wonder if my advice is worth while.
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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