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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
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"
 

help_questions

Board Regular
Joined
Aug 22, 2005
Messages
215
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?
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
Sorry, Have to run now. Will check this post tomorrow. Maybe someone can answer before then.
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985

ADVERTISEMENT

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.
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
Glad it worked for you.
Thanks for the follow up, always wonder if my advice is worth while.
 

Forum statistics

Threads
1,141,718
Messages
5,708,072
Members
421,543
Latest member
SGM

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
Top