FileCopy with Todays Date

sunrunner4kr

Registered User.
Local time
Today, 16:12
Joined
Apr 24, 2008
Messages
16
Hi,

I want at a click of a button, to copy a file and rename it with Todays date.

C:\FILES\ALLOC.csv
C:\FILES\ALLOC191109.csv

I've got the FILECOPY code working:

Private Sub rename_Click()

FileCopy "C:\FILES\ALLOC.CSV", "C:\FILES\ALLOC191109.csv"

End Sub

but I've tried adding the date, and I'm not sure where I'm going wrong.

Private Sub rename_Click()

Dim NewName As String

NewName = "C:\FILES\ALLOC" & Date & ".CSV"
FileCopy "C:\FILES\ALLOC.CSV", NewName


End Sub

Any help would be greatly appreciated. Thanks.
 
You haven't mentioned what error you're getting or at what line of code!

My guess is that the value of NewName just after this line executes ...
Code:
NewName = "C:\FILES\ALLOC" & Date & ".CSV"
... is an illegal filename.
 
Sorry...

...error highlighted on:

FileCopy "C:\FILES\ALLOC.CSV", NewName

Runtime error 76: Path not found.

wasn't sure if the FileCopy function allowed for a variable string.

Cheers
 
So did you solve this? The date function will return a value like 11/19/09 and those slash characters "/" are illegal in a filename.
You could, among other things, use the Replace() function ...
Code:
NewName = "C:\SomePath\SomeFile_" & Replace(Date, "/", "-") & ".csv"
debug.print NewName
You should get ....
Code:
C:\SomePath\SomeFile_11-19-09.csv
 
To improve on your code you would be better converting the date element of the file name to

Format(Date(),"yymmdd")

That way when you read the files in your folder they would appear in the correct order, when sorted.

David
 
NewName = "C:\SomePath\SomeFile_" & Format(Date(),"yyyymmdd")
& ".csv"

should work.
 

Users who are viewing this thread

Back
Top Bottom