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

Filename With the Date 1

Status
Not open for further replies.

samuelma

MIS
Jun 5, 2001
16
0
0
US
I have this script that copies a file from one place to another. I use this to copy files over the network to my machine or to another place. I use this to automate coping many files like a batch file.
I need to time stamp the file name I'm copying to. So it needs to be done at the "fso.CreateTextFile" and at the f2.copy I think. I have no clue!

Code:
Dim fso, f1, f2
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\temp\testtest{date}.txt")
f1.close
Set f2 = fso.getfile ("C:\SamsVBS\test.txt")
f2.copy ("c:\temp\testtest{date}.txt")
 
Try using it with this function:

*******************************************
function ReturnTimeStamp(myPath,myExtension)
Dim myMonth,myDay,myYear,myDate

myMonth=right("0" & month(now),2)
myDay=right("0" & day(now),2)
myYear=right("0" & year(now),4)
myDate=myYear & MyMonth & myDay
ReturnTimeStamp=myPath & myDate & myExtension

end function
*******************************************

This is an idea of how to use it:

msgbox ReturnTimeStamp("C:\mydir\db",".txt")
 
The message box thing is very cool.
I've tried adding it to the file name in many ways.
Set f1 = fso.CreateTextFile ReturnTimeStamp("c:\temp\testtest",".txt")
Set f1 = fso.CreateTextFile("c:\temp\testtest&mydate&.txt")
Set f1 = ReturnTimeStamp.fso.CreateTextFile("c:\temp\testtest",".txt")

Lets just say I'm a little inexpieranced with VBS. I am trying to make it work but I'm missing something about having the time stamp land in the file name.
 
If you're just trying to copy files try this:

dim fs, myFile
set fs = CreateObject ("Scripting.FileSystemObject")
set myFile = fs.GetFile("c:\somefile.txt")
myFile.Copy(ReturnTimeStamp("c:\XX",".txt"))


function ReturnTimeStamp(myPath,myExtension)
Dim myMonth,myDay,myYear,myDate

myMonth=right("0" & month(now),2)
myDay=right("0" & day(now),2)
myYear=right("0" & year(now),4)
myDate=myYear & MyMonth & myDay
ReturnTimeStamp=myPath & myDate & myExtension

end function
 
I had to use 2 sets of parens. Imagin that, DOH!
Like the following..
Code:
function ReturnTimeStamp(myPath,myExtension)
Dim myMonth,myDay,myYear,myDate
    myMonth=right("0" & month(now),2)
    myDay=right("0" & day(now),2)
    myYear=right("0" & year(now),2)
    myDate=  MyMonth & "-" &myDay & "-" &myYear
    ReturnTimeStamp=myPath &"-" & myDate & myExtension
end function
Dim fso, f1, f2
Set f1 = fso.CreateTextFile (ReturnTimeStamp("C:\My Reports\myfile-",".txt"))
f1.close
Set f2 = fso.getfile("\\server\share\folder\folder2\folder3\results.txt")
f2.copy (ReturnTimeStamp("C:\My Reports\myfile-",".txt"))

It is working great thanks for all the help to everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top