Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-26-2012, 01:13 PM
ddnllc ddnllc is offline
Registered User
 
Join Date: Apr 2012
Posts: 7
Save Output file with Time/Date Stamp

Hello,

Is it possible to save an output file with a date and/or time stamp?

For example, I am using below and would like to add uniqueness to the file name by adding the date and/or time.

Set objFile3 = fso.OpenTextFile("C:\temp\output_results.txt", ForWriting, True)

I would like to use something similar to this:

Set objFile3 = fso.OpenTextFile("C:\temp\output_results_" & date & "_.txt", ForWriting, True)

Or if there is a way to add some sort of incremental revision to the file name.

Thanks in advance.
Reply With Quote
  #2  
Old 07-26-2012, 02:40 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Since this is a VBScript oriented question, rather than SecureCRT specific scripting question, you could probably find lots of ideas and examples by searching the Internet for VBScript get date example.

The Microsoft VBScript help (see Script56.chm) contains the following example.
Code:
function DateDemo(){
   var d, s = "Today's date is: ";           //Declare variables.
   d = new Date();                           //Create Date object.
   s += (d.getMonth() + 1) + "/";            //Get month
   s += d.getDate() + "/";                   //Get day
   s += d.getYear();                         //Get year.
   return(s);                                //Return date.
}
Does anyone else have ideas or examples they care to share?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 07-26-2012, 05:28 PM
ddnllc ddnllc is offline
Registered User
 
Join Date: Apr 2012
Posts: 7
Thanks Mike. That steered me in the right direction for what I needed. Below is a sample of code that I included in my script. Maybe someone else will be able to use it. Thanks again.



#$language = "VBScript"
#$interface = "1.0"

Sub Main

crt.Screen.Synchronous = True

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim strDate1
Dim strTime1
Dim strTime2

Dim myDateString0, myDateString1, myDateString2, myDateString3, myDateString4
Dim myTimeString0, myTimeString1, myTimeString2, myTimeString3, myTimeString4

' -----> 0 (used for dates) - display a date in the format mm/dd/yy (default)
' -----> 1 (used for dates) - display a date in the format day, month and month day, year
' -----> 2 (used for dates) - display a date in the format mm/dd/yy (default (same as 0))
' -----> 3 (used for time) - display time in the format hh:mm:ss: PM/AM (12 hour format)
' -----> 4 (used for time) - display time in the format hh:mm (24 hour format)


myDateString0 = FormatDateTime(Date(), 0)
myDateString1 = FormatDateTime(Date(), 1)
myDateString2 = FormatDateTime(Date(), 2)
myDateString3 = FormatDateTime(Date(), 3)
myDateString4 = FormatDateTime(Date(), 4)

myTimeString0 = FormatDateTime(Time(), 0)
myTimeString1 = FormatDateTime(Time(), 1)
myTimeString2 = FormatDateTime(Time(), 2)
myTimeString3 = FormatDateTime(Time(), 3)
myTimeString4 = FormatDateTime(Time(), 4)


strDate1 = Left(myDateString1, 3)
strTime1 = Left(myTimeString4, 2)
strTime2 = Right(myTimeString4, 2)

MsgBox strDate1
MsgBox strTime1
MsgBox strTime2



Set objFile3 = fso.OpenTextFile("C:\temp\timedate_outputresults_" & strDate1 & "_" & strTime1 & "-" & strTime2 & ".txt", ForWriting, True)


End Sub
Reply With Quote
  #4  
Old 07-27-2012, 01:00 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Thanks ddnllc, I'm glad the information helped - thanks for posting your script!
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 07-27-2012, 01:53 PM
emailsbecker emailsbecker is offline
Registered User
 
Join Date: Apr 2011
Posts: 74
Thought I would share what I've cobbled together. I prefer my logs in the format of:

Code:
YYYY-MM-DD.HH-MM-SS.HOSTNAME.log
In order to be able to sort the files properly the dates and times have to be 2-digit characters. Here's what I came up with. Also note the file I save logs to. The "00 - " at the start ensures it's the first item in the folder list. Every time I have to go to that folder and open a log (or attach a log to an email, etc) having that right at the top saves me time. Only a second at a time, but it adds up. In my scripts folder I have 7-8 scripts I use most often, I used the same trick on their files names for the same reason.

Code:
# $language = "VBScript"
# $interface = "1.0"

  crt.Screen.Synchronous = True

  Dim logfile, y, m, d, h, n, s, MyDate

  y = Year(Date) 
  m = Month(Date) : If Len(m)=1 Then m = "0" & m : End If
  d = Day(Date) : If Len(d)=1 Then d = "0" & d : End If
  h = Hour(Time) : If Len(h)=1 Then h = "0" & h : End If
  n = Minute(Time) : If Len(n)=1 Then : n = "0" & n : End If
  s = Second(Time) : If Len(s)=1 Then : s = "0" & s : End If
  MyDate=y&"."&m&"."&d&"-"&h&"."&n&"."&s

sub Main

  ' If folder doesn't exist create it
  '
  Dim filesys, newfolder
  set filesys=CreateObject("Scripting.FileSystemObject")
  If  Not filesys.FolderExists("C:\00 - SecureCRT Logfiles") Then
     newfolder = filesys.CreateFolder ("C:\00 - SecureCRT Logfiles")
  End If

  ' Get hostname to be used in the logfile filename
  '
   crt.Screen.Send "show system" & vbCr : crt.Screen.WaitForString vbCr
   strHNPre = crt.Screen.ReadString ("#")
   strHN = Split(Mid(strHNPre, 32), vbcrlf)(0)
   
  ' Set logfile name & turn on logging
  '
  logfile = "C:\00 - SecureCRT Logfiles\" & MyDate & "-" & strHN & ".log"
  crt.Session.LogFileName = logfile
  crt.Session.Log True


  '
  '
  ' do stuff here
  '
  '

  ' Stop logging
  '
  crt.Session.Log False

End sub

Last edited by emailsbecker; 07-27-2012 at 03:39 PM.
Reply With Quote
  #6  
Old 07-27-2012, 02:58 PM
ddnllc ddnllc is offline
Registered User
 
Join Date: Apr 2012
Posts: 7
Thanks becker. Ive modified your script to suit my needs. I like your format much better. I broke the date and time into two different strings. I usually save my code in such a way that I can just add it to various scripts as needed. Sometimes I only need date or time, other occasions I need both. See below.

#$language = "VBScript"
#$interface = "1.0"

Sub Main

crt.Screen.Synchronous = True

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1, ForWriting = 2, ForAppending = 8


Dim strYear, strMonth, strDay, strHour, strMinute, strSecond, strMyDate, strMyTime



strYear = Year(Date)
strMonth = Month(Date) : If Len(m)=1 Then m = "0" & m : End If
strDay = Day(Date) : If Len(d)=1 Then d = "0" & d : End If
strHour = Hour(Time) : If Len(h)=1 Then h = "0" & h : End If
strMinute = Minute(Time) : If Len(n)=1 Then : n = "0" & n : End If
strSecond = Second(Time) : If Len(s)=1 Then : s = "0" & s : End If
strMyDate = strYear & "." & strMonth & "." & strDay
strMytime = strHour & "." & strMinute & "." & strSecond





Set objFile3 = fso.OpenTextFile("C:\temp\outputresults-" & strMyDate & "_" & strMyTime & ".txt", ForWriting, True)

End Sub
Reply With Quote
  #7  
Old 07-27-2012, 03:18 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Quote:
emailsbecker: after playing with the code some more I see there's a problem. SecureCRT doesn't create a new logfile each time. Even though I have a MsgBox confirm the new date/time after setting the "logfile = " command the script keeps reusing a file name I created hours ago.... Any thoughts Mike?
I haven't tested the code yet so I'm not certain about the problem/fix, but I might see the cause. If you turn off logging by adding
crt.Session.Log False
before changing the filename (logfile =), does that help? Assuming logging is always disabled before you start the script, you shouldn't need it. The bugs I create are often born in my assumptions.

Also, I don't exactly see where logfile is used in setting the SecureCRT log filename. Is there more code which does something similar to the following?
crt.Session.LogFileName = logfilename
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #8  
Old 07-27-2012, 03:38 PM
emailsbecker emailsbecker is offline
Registered User
 
Join Date: Apr 2011
Posts: 74
Quote:
Originally Posted by miked View Post
Also, I don't exactly see where logfile is used in setting the SecureCRT log filename. Is there more code which does something similar to the following?
crt.Session.LogFileName = logfilename
>_< !!!

In copying and pasting things around I must've left that line out. Thanks. I knew I was overlooking something!

I've editied the code above to add that in and removed my question about it. My code above now works as intended.

Quote:
Originally Posted by ddnllc View Post
Thanks becker. Ive modified your script to suit my needs.
... as I did with code before me. Glad you found my addition (the format) useful.

Last edited by emailsbecker; 07-27-2012 at 03:44 PM.
Reply With Quote
  #9  
Old 07-27-2012, 03:44 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Excellent!
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #10  
Old 07-30-2012, 01:02 AM
emailsbecker emailsbecker is offline
Registered User
 
Join Date: Apr 2011
Posts: 74
I was looking in the scripting guide PDF for something else and happened to stop on page 84. Turns out a whole bunch of that code was unnecessary. This does the same job:

Code:
# $language = "VBScript"
# $interface = "1.0"

  crt.Screen.Synchronous = True

sub Main

  ' If folder doesn't exist create it
  '
  Dim filesys, newfolder
  set filesys=CreateObject("Scripting.FileSystemObject")
  If  Not filesys.FolderExists("C:\00 - SecureCRT Logfiles") Then
     newfolder = filesys.CreateFolder ("C:\00 - SecureCRT Logfiles")
  End If

  ' Get hostname to be used in the logfile filename
  '
   crt.Screen.Send "show system" & vbCr : crt.Screen.WaitForString vbCr
   strHNPre = crt.Screen.ReadString ("#")
   strHN = Split(Mid(strHNPre, 32), vbcrlf)(0)
   
  ' Set logfile name & turn on logging
  '
  logfile = "C:\00 - SecureCRT Logfiles\%Y.%M.%D-%h.%m.%s-" & strHN & ".log"
  crt.Session.LogFileName = logfile
  crt.Session.Log True


  '
  '
  ' do stuff here
  '
  '

  ' Stop logging
  '
  crt.Session.Log False

End sub
Reply With Quote
  #11  
Old 07-30-2012, 11:11 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
SecureCRT's logfile variables (%Y.%M.%D-%h.%m.%s) can simplify code.

If you were going to log data yourself instead of using SecureCRT, or if you need to generate the date for any other reason, then knowing how to use Year(Date), Month(Date), etc., can be helpful.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #12  
Old 07-31-2012, 07:09 AM
emailsbecker emailsbecker is offline
Registered User
 
Join Date: Apr 2011
Posts: 74
That's true. In some of our operations we do save config files on devices, in the future I might need to script something to do that automatically. In that case my original code would be needed. But as long as it's just referring to the SecureCRT logfile the % stuff is all we need right now.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 06:35 PM.