Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename a file with current YYYYMMDD in the filename 2

Status
Not open for further replies.

jpotucek

Technical User
Jan 26, 2005
144
US
Let me start off by saying that I am not a VB programmer.. but this forum has helped me out before so I'm hoping that I can get some help again !!!

I need to rename a file on a daily basis so that the filename contains the current date in YYMMDD format.

my original filename is FILENAME_S.csv and I need to rename it to
NEWFILENAME_YYYYMMDD.csv

Can this be done programmatically with some basic VB code????

I need to create this in a script so that I can run it once a day via windows scheduler

any help would be greatly appreciated!
 
i came up with this.. but it is erroring out on line 1 character 12 'expected end of statement' not sure what is wrong ????

Dim myFile as String
Dim myNewFile as String
myfile = "C:\filename_s.csv"
myNewFile = "C\newfilename" & Format(Date, "yyyymmdd") & ".csv"
Name myfile As myNewFile
 
VBScript don't have typed variable.
Your code is VBA, not VBS.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Try something like this....

Uncle Mike



Dim file1, file2

file1 = "C:\Oldfile.txt"
file2 = "C:\newfile" & year(date) & month(date) & Day(Date) & ".txt"

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(file1) = true then
If fso.FileExists(file2) = true then
fso.DeleteFile(file2)
End if
fso.MoveFile file1, file2
'else
' msgbox "File does not exist"
End If

Set FSO = Nothing
 
So, mike has followed my suggestion but omitted the Right function:
file2 = "C:\newfilename" & Year(Date) & [!]Right("0" & [/!]Month(Date)[!],2)[/!] & [!]Right("0" & [/!]Day(Date)[!],2)[/!] & ".csv"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Do as PHV has suggested to get better looking output.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thank you !!!! EXACTLY WHAT I NEEDED!!! One problem though. It created my filename as newfilename200757.csv should be newfilename20070507.csv ?????
 
Seems you haven't read my post stamped 4 May 07 15:46.
 
Thanks Guys. Worked like a charm!!! You're awesome!!!
 
PHV said:
Right("0" & Month(Date),2)
Never seen that method of pading before but I plan on using it. Have a star from me PHV.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top