Back up a folder that contains subfolders and files

zelarra

Board Regular
Joined
Jan 2, 2021
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hello.

I'm looking for a way to backup a folder. This contains subfolders at various levels and files. I have created an example folder for you. I also copy the code that I use and it does not copy everything, only the folders with a single level.

VBA Code:
Sub CopiarDocumentacion()

Application.ScreenUpdating = False

Application.DisplayAlerts = False

On Error Resume Next

Dim fso As Object
Dim CarpetaOrigen As String
Dim CarpetaDestino As String

Set fso = CreateObject("Scripting.FileSystemObject")

CarpetaOrigen = "C:\Users\Diego\Escritorio\Documentación"
CarpetaDestino = "C:\Users\Diego\Escritorio\Organizar\Nuevo. Guardar\00 Copia de seguridad\Documentación"

fso.CopyFolder CarpetaOrigen, CarpetaDestino

MsgBox ("La carpeta se copió con éxito."), vbInformation, "Copia de seguridad"

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub

I want it to copy all the files except the shortcuts and, importantly, if a file is open, to copy it too. With the latter I had problems with this code that I copy below, one of the times I copied it, someone on another computer different from mine (it is shared on the network) had it open and it gave me an error.


Code:
Sub CopiarVacaciones()

    Dim RutaOrigen As String
    Dim RutaDestino As String
    
    ' Rutas de origen y destino
    RutaOrigen = Range("I1").Value
    RutaDestino = "C:\Users\20ZU0050\Desktop\00 Copia de seguridad\Vacaciones 2022.xlsx"
    
    ' Copiar el archivo
    FileCopy RutaOrigen, RutaDestino

MsgBox "Hemos copiado la ruta del archivo " & RutaOrigen & " en la nueva ubicación (" & RutaDestino & ").", vbInformation, "Copia de seguridad"

End Sub


Thank you so much.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
This is what I have done so far, but I can't get it to work. I want that, when copying the folder called Excel, it only copies the files in the Excel folder of the 00 Backup folder created for this purpose (without subfolders). Outside of there, you have to copy everything: files, folders and subfolders. I don't want to personalize the folder with a name that only has to copy the files (without subfolders) in case I want to change the name or have others. As you can see, I am creating a procedure that works for both things. However, it creates an infinite loop that I am able to solve.


Let's see if someone is encouraged and helps me, please.

Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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