Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Time stamp a file.

545 views
Skip to first unread message

TimM

unread,
Feb 26, 2008, 4:03:01 PM2/26/08
to
Hello all. I have the following script segment that creates a time stamped
string for use in file or folder names. but need to do it in an easier way,
or at least in less code. Any ideas?

datetoday = Now
strMonth = Month(datetoday)
strDay = Day(datetoday)
strHour = Hour(datetoday)
strMinute = Minute(datetoday)
strSecond = Second(datetoday)
if len(strMonth) = 1 Then
strMonth = "0" & strMonth
end If
if len(strDay) = 1 Then
strDay = "0" & strDay
end If
if len(strHour) = 1 Then
strHour = "0" & strHour
End If
If len(strMinute) = 1 Then
strMinute = "0" & strMinute
End If
If len(strSecond) = 1 Then
strSecond = "0" & strSecond
End If
datetoday = YEAR(datetoday) & strMonth & strDay
timetoday = strHour & strMinute & strSecond
strFile = datetoday & "_" & timetoday & ".zip"
FSO.CreateTextFile(strFile)

--
Thanks
TimM

Tom Lavedas

unread,
Feb 26, 2008, 4:20:51 PM2/26/08
to

This is about how I generally do it ...

datetoday = Now
strMonth = Right("0" & Month(datetoday) ,2)
strDay = Right("0" & Day(datetoday) ,2)
strHour = Right("0" & Hour(datetoday) ,2)
strMinute = Right("0" & Minute(datetoday),2)
strSecond = Right("0" & Second(datetoday),2)


datetoday = YEAR(datetoday) & strMonth & strDay
timetoday = strHour & strMinute & strSecond
strFile = datetoday & "_" & timetoday & ".zip"
FSO.CreateTextFile(strFile)

HTH

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Pegasus (MVP)

unread,
Feb 26, 2008, 4:24:15 PM2/26/08
to

"TimM" <Ti...@discussions.microsoft.com> wrote in message
news:6541040F-8C3A-48C1...@microsoft.com...

You could do it like so:

strFile = Year(Now) & Pad(Month(Now)) & Pad(Day(Now)) & "_" _
& Pad(Hour(Now)) & Pad(Minute(Now)) & Pad(Second(Now)) & ".zip"
FSO.CreateTextFile("d:\" & strFile)

Function Pad(n)
If n < 10 Then
Pad = "0" & n
Else
Pad = n
End If
End Function


McKirahan

unread,
Feb 26, 2008, 4:40:16 PM2/26/08
to
"TimM" <Ti...@discussions.microsoft.com> wrote in message
news:6541040F-8C3A-48C1...@microsoft.com...

Presuming you want "yyyymmdd_hhnnss.zip", then:

dtm = Now
ymd = (Year(dtm)*10000) + (Month(dtm)*100) + Day(dtm)
hms = (Hour(dtm)*10000) + (Minute(dtm)*100) + Second(dtm)
strFile = ymd & "_" & hms & ".zip"
FSO.CreateTextFile(strFile)


ekkehard.horner

unread,
Feb 26, 2008, 5:04:55 PM2/26/08
to
TimM schrieb:

> Hello all. I have the following script segment that creates a time stamped
> string for use in file or folder names. but need to do it in an easier way,
> or at least in less code. Any ideas?
>
[...]
If you can use .NET (runtime installed), you could use the function

'' DNIPrintF( sFmt, aItems ) - use .NET to sprintf()
' ----------------------------------------------------------------------------
Function DNIPrintF( sFmt, aItems )
Dim oSB : Set oSB = CreateObject( "System.Text.StringBuilder" )
oSB.AppendFormat_4 sFmt, (aItems)
DNIPrintF = oSB.ToString()
End Function

to solve you problem like this:

Dim sFiNa : sFina = DNIPrintF( "{0:yyyyMMdd-HHmmss}.zip", Array( Now() ) )

Of course, after that you won't have to do other formatting/complex
concatenation tasks 'by hand' evermore.

0 new messages