How to rename a folder added to the current date to the name

1

I have a folder where I keep data that needs to be updated daily. In order not to lose the history I copy this folder and paste it in another place. But after copying and pasting the folder, I need to rename it with the current date so I do not have folders with the same name.

Sub faz_backup()
Dim newName As String

CopiarArq "C:\Users\leandro.lazari\Desktop\Dados MegaWhat\Dados", 
"C:\Users\leandro.lazari\Desktop\Dados MegaWhat\Histórico"

Data = DateSerial(Year(Now), Month(Now), Day(Now))
newName = "C:\Users\leandro.lazari\Desktop\Dados MegaWhat\Histórico\Dados"

Name "C:\Users\leandro.lazari\Desktop\Dados MegaWhat\Histórico\Dados" 
As newName & Data

End Sub

I did it this way. The code copies and pastes the folder, but an error appears saying that the file was not found. However, the address is correct

    
asked by anonymous 23.03.2018 / 14:59

1 answer

1

You're experiencing an error because String Data returns the value: 03/23/2018. When you try to change the name of a folder manually and attempt to insert / , the following message appears:

Soyoushouldreplacethe/withsomeothercharacter,suchas-

Usingthereplacefunction,thefollowingcodecanbeused:DatasemBarra=Replace(Data,"/", "-")

Then the folder can be renamed with Name "C:\MeuCaminho\Desktop\Dados" As newName & DatasemBarra

Complete Code

Dim newName As String
Dim FSO As Object

Set FSO = CreateObject("scripting.filesystemobject")

Data = DateSerial(Year(Now), Month(Now), Day(Now))
DatasemBarra = Replace(Data, "/", "-")
newName = "C:\Users\MeuPC\Desktop\Dados"

If FSO.FolderExists(newName) = False Then
    MsgBox newName & " não existe."
    Exit Sub
Else
    Name "C:\Users\MeuPC\Desktop\Dados" As newName & DatasemBarra
End If

And an option not to use the filesystemobject object:

Dim newName As String

Data = DateSerial(Year(Now), Month(Now), Day(Now))
DatasemBarra = Replace(Data, "/", "-")
newName = "C:\Users\Usuario\Desktop\Dados"

 If Dir(newName, vbDirectory) = "" Then
    MsgBox newName & " não existe."
    Exit Sub
Else
    Name newName As newName & DatasemBarra
End If
    
23.03.2018 / 21:07