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

date/time stamp

Status
Not open for further replies.

mg911

Technical User
Jul 18, 2001
59
US
is there a way to have a file (ex. smith.log) and rename it to smith1205011200.log where the format is mm/dd/yy/hh/mm.log

thanks

 
mg911,

dd = Day(date)
mm = Month(date)
yy = Year(date)
strMonth = MonthName(mm, true)

However, I wouldn't put a slash (/) mark as part of the name of a file. It may not be compatible since slashes are used to denote directories.

fengshui_1998
 
what i mean to say was - is there a way using VB script to have a file renamed using that format -

so using VB or VBS can i have a file named joe.log and rename it to joe12345.log using vb or vbs

Thanks

Mike
 



mg911,


Set fso = CreateObject("Scripting.FileSystemObject")
Set myFile = fso.GetFile("c:\yourfile.txt")
myFile.Move "c:\windows\desktop\"

fengshui_1998
 
so is there a way to add a date/time stamp to that file that you renamed?

 

dd = Day(date)
mm = Month(date)
yy = Year(date)
strMonth = MonthName(mm, true)

Set fso = CreateObject("Scripting.FileSystemObject")
Set myFile = fso.GetFile("c:\yourfile.txt")
myFile.Move "c:\path\" & dd & strMonth & yy & ".txt"

The above code will rename your file for you as long as the path is valid. It will look like:

6DEC2001.txt


fengshui_1998



 
"is there a way to have a file (ex. smith.log) and rename it to smith1205011200.log where the format is mm/dd/yy/hh/mm.log"

to get EXACTLY the format you requested:

DatePart("M", NOW) = today's month
DatePart("D", NOW) = today's day
DatePart("YYYY", NOW) = today's year
Right(DatePart("YYYY", NOW), 2) = last 2 of today's year
DatePart("H", NOW) = today's hour
DatePart("N", NOW) = today's minute

so, assuming sName = "smith" or some other variable name...

myFile.Move "c:\path\" & sName & DatePart("M", NOW) & DatePart("D", NOW) & Right(DatePart("YYYY", NOW), 2) & DatePart("H", NOW) & DatePart("N", NOW) & ".txt"

or...... could make it even simpler (if you don't mind the seconds being in there, too...

'format date/time as mm/dd/yy hh:mm:ss
'strip out /'s
sDate = Replace(FormatDateTime(NOW, 0), "/", "")
'strip out spaces
sDate = Replace(sDate, " ", "")
'strip out :'s
sDate = Replace(sDate, ":", "")

myFile.Move "c:\path\" & sName & sDate & ".txt"


would name the file:

c:\path\smith120701103301.txt

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top