To rename a folder added the current date to the name

Asked

Viewed 1,216 times

1

I have a folder where I keep data that needs to be updated daily. To not miss the history copy this folder and paste elsewhere. But, after copying and pasting the folder, I need to rename it with the current date to 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 so. The code copies and pastes the folder, but an error appears saying that the file was not located. However, the address is correct

1 answer

1


You are in error because the String Data returns the value: 03/23/2018. When you try to change a folder name manually and try to insert /, the following message appears:

mensagem de erro renomear arquivos caracteres especiais

Therefore you must replace the / by some other character, such as -

Using the replace function, the following code can be used: 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
  • Daniel, good morning! I made the changes you said and tried to perform a few times. Sometimes it works and sometimes the error appears: "Run-time error '75': Path/file access error". Some reason for this?

  • On which line the error occurs?

  • Where renames the folder, in the name line "..." as "...."

  • Well I can’t reproduce this error, but do you have any user restrictions? Is it a company computer? You may not be allowed access or the Path is incorrect. But if you used the full code that checks for the folder, the problem would probably be permission.

  • If the user does not have access to make some changes, mainly on the server... IT can help you with access restrictions.

  • Got it! But if I didn’t have access permission I wouldn’t have trouble doing it manually either?

  • Behold this article and check that the folder is not read-only. And your account user permissions. Or you may be entering a wrong path, such as by inserting the \ at the end of the newname string

  • Daniel, you didn’t change the outcome.

  • Or you may be entering a wrong path, for example by inserting the bar at the end of the newname string. But check the permissions.

  • 1

    Daniel, I was able to fix it! The problem was time. The code tried to rename the file before it appeared in the folder. I put a Sleep remote and it worked.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.