RSS

VBScript: Adding date/time stamp to log file name

16 Oct

Very often when we do logging for our script we would also want to apply a date/time stamp to them. Unfortunately VB does no offer any easy way to do this. This is how I do it:

LogFile = GetScriptPath() & "iStar_" & _
 cStr(Year(now())) & Pad(cStr(Month(now())),2) & _
 Pad(cStr(Day(now())),2) & Pad(cStr(Hour(now())),2) & _
 Pad(cStr(Minute(now())),2) & Pad(cStr(Second(now())),2) & _
 ".log"

You will see an additional function Pad(), this is necessary to ensure constant number of strings. The function simply added addition “0” for single digit numbers to make them 2 digits. It is of course a useful function for other stuff.

Function Pad(CStr2Pad, ReqStrLen)
Dim Num2Pad

 Pad = CStr2Pad
 if len(CStr2Pad) < ReqStrLen then
 Num2Pad = string((ReqStrlen - len(CStr2Pad)), "0")
 Pad = Num2Pad & CStr2Pad 
 end if
End Function
Advertisements
 
2 Comments

Posted by on October 16, 2012 in Scripts, VBScripts

 

2 responses to “VBScript: Adding date/time stamp to log file name

  1. DK

    September 11, 2014 at 11:43 pm

    Hi.
    Can you please help, or point to right direction, how I can modify existing .VBS script used for CRT , in order add timestamp to existing string in code below?
    This way, every time I run the script, file will be send to FTP server with unique identifier – timestamp.
    Below is the portion CRT script , in which “_xx:yy:zz” needs to be added it with current time, every time script run.

    #$language = “VBScript”
    #$interface = “1.0”

    crt.Screen.Synchronous = True

    ‘ This automatically generated script may need to be
    ‘ edited in order to work correctly.

    Sub Main

    ………………………………………………………………………….
    crt.Screen.Send “run file copy /var/log/messages destination ftp://linux@11.11.11.11/logsStorage/messages_xx:yy:zz” & chr(13)
    crt.Screen.WaitForString “DUT# ”
    ………………………………………………………………………….
    End Sub

     
  2. Kelvin Wong

    September 12, 2014 at 10:17 am

    You can do something like this:

    hr = Pad(cStr(Hour(now())),2)
    min = Pad(cStr(Minute(now())),2)
    sec = Pad(cStr(Second(now())),2)

    then

    crt.Screen.Send “run file copy /var/log/messages destination ftp://linux@11.11.11.11/logsStorage/messages_” & hr & “:” & min & “:” & sec & chr(13)
    crt.Screen.WaitForString “DUT# ”

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: