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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy and Rename a file

Status
Not open for further replies.

ckeener

Programmer
Dec 2, 2003
53
US
I am fairly new to VBScript and I am trying to write a script that will copy a file (AUTO.MON) to a specified folder and rename it (ASPLAY040317.txt) The 040317 part of the title would be for the file copied over on March 17, 2004. The code that I have is this.
Code:
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "Z:\Dad\ASPLAY\AUTO.MON", "C:\ASPLAY" & 
     Now() & ".txt"

This, however, thinks that I am trying to tell it to go to a folder that it can't find.

How do I tell it to rename the file once it is copied??

Thanks, Caleb
 
NOW() is going to be resolved into a text string (it begins life as a datetime) according to what you have set in your locale settings. On my system, right NOW, that corresponds to "03/17/04 5:10:40 PM". You are therefore trying to create a file called

C:\ASPLAY03/17/04 5:10:40 PM.txt

Clearly not a valid file name. You probably need to explicitly format NOW() to something that's valid in a file name.
 
Good point Golom,

My question then would be is my code a valid way to change the name of a file as I copy it and if not how would I change the name of the file once I have copied it over.

Thanks, Caleb
 
Hi Ckeener
It fails because you are not allowed "/" or ":" in a vadid filename, you could change it to change the "/" for a "-" and the ":" for a "." and use something like this.

Dim FSO
Dim XDate, XTime
Set FSO = CreateObject("Scripting.FileSystemObject")

XDate=Mid(Date,1,2) & "-" & Mid(Date,4,2) & "-" & Mid (Date,7,4)
XTime=" " & Mid(Time,1,2) & "." & Mid(Time,4,2)
FSO.CopyFile "I:\AUTO.MON", "I:\TEMP\ASPLAY" & XDate & XTime ".TXT"

Hope this helps

 
Thanks to both of you,

You were both very helpful. This is the code that works for me now.

Code:
'***************************************************************
'***************************************************************
'* This Batch File must be run the day on the day after whatever 
'* ASPLAY list that is to be copied was created.  It will copy
'* the ASPLAY and ONAIR lists and rename them to be text files
'* and have the date included in the title - ASPLAY_YYMMDD.txt
'***************************************************************

Dim FSO, thisday
Set FSO = CreateObject("Scripting.FileSystemObject")
Today_Date()
FSO.CopyFile "Z:\Dad\ASPLAY\AUTO.FRI", "Z:\ASPLAY\FRIDAY\ASPLAY_" & thisday & ".txt"
FSO.CopyFile "Z:\Dad\ASPLAY\ONAIR.FRI", "Z:\ASPLAY\FRIDAY\ONAIR_" & thisday & ".txt"

' ***************************************************************
' ### This funtion gets the date in format yymmdd ###
'*************************************************************** 
Function Today_Date()

thisday=Right(Year(Date),2) & Right("0" & Month(Date),2) & Right("0" & Day(Date - 1),2)

End Function
' ***************************************************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top