none
Timestamp required in output file within VBScript RRS feed

  • General discussion

  • Hi Guys,

    I have designed a script which is working fine and creates a log file for every command executed from input file. Now what I want is to insert current timestamp  before every entry in output file. Like mentioned below:

    ---------------------------------------------------------
    2014-11-16 09:38:17 Command: COPY
    2014-11-16 09:38:17 Response: Command executed.
    2014-11-16 09:38:18 Status: Files copied successfully.
    ---------------------------------------------------------

    I wrote a below command for this but it's not working.

    Set Exec = Shell.Run(StrCommand)
    dt = Now()
    textFile.WriteLine Year(dt) & "-" & Right("0" & Month(dt),2) & "-" & Right("0" & Day(dt),2) & "    " & Right("0" & Hour(dt),2) & ":" _
                 & Right("0" & Minute(dt),2) & ":" & Right("0" & Second(dt),2) & "-----   " & Exec.StdOut.WriteLine 

    Could anyone of you guide me where I'm wrong? or can provide me sample code for this will be really appreciated. Thanks


    PT

    Sunday, November 16, 2014 7:23 AM

All replies

  • Why work so hard.  Try this.

    Function LogMsg(msg)
       dt=Replace(Now,"/","-")
       WScript.Echo "[" & dt & "]" & msg
    End Function

    WScript.Echo _
        Year(Now) & "-" _
        & Right("0" & Month(Now) ,2) & "-" _
        & Right("0" & Day(Now)   ,2) & " " _
        & Right("0" & Hour(Now)  ,2) & ":" _
        & Right("0" & Minute(Now),2) & ":" _
        & Right("0" & Second(Now),2)


    ¯\_(ツ)_/¯



    • Edited by jrv Saturday, December 13, 2014 2:02 PM
    Sunday, November 16, 2014 8:15 AM
  • Unable to catch above code

    Function LogMsg(msg)
       dt=Replace(Now,"/","-")
       WScript.Echo "[" & dt & "]" & msn
    End Function

    msg and msn ?how to implement this in my code? Please clear my confusion.


    PT

    Sunday, November 16, 2014 12:31 PM
  • Sorry - typo. Al fixed now.

    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, November 16, 2014 2:05 PM
    Sunday, November 16, 2014 2:04 PM
  • Hi Guys,

    I want to insert timestamp before every line output file. Which will help us in tracking that what time command executed.  Any help will be appreciated. 

    Need a log file as 

    --------------------------------------------------------------------------

     2014-12-12 00:24:42AM Session name: Test@ftp.Demo.net (Ad-Hoc site)
     2014-12-12 00:24:42AM Host name: ftp.Demo.net (Port: 22)
     2014-12-12 00:24:43AM User name: Test (Password: No, Key file: No)
     2014-12-12 00:24:43AM Tunnel: No
     2014-12-12 00:24:43AM Transfer Protocol: SFTP (SCP)
     2014-12-12 00:24:44AM Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
     2014-12-12 00:24:44AM Proxy: none
     2014-12-12 00:24:44AM Send buffer: 262144
     2014-12-12 00:24:45AM SSH protocol version: 2; Compression: No
     2014-12-12 00:24:45AM Bypass authentication: No
     2014-12-12 00:24:45AM Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
     2014-12-12 00:24:46AM Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
     2014-12-12 00:24:46AM SSH Bugs: A,A,A,A,A,A,A,A,A,A
     2014-12-12 00:24:46AM Simple channel: Yes
     2014-12-12 00:24:47AM Return code variable: Autodetect; Lookup user groups: A
     2014-12-12 00:24:48AM Shell: default
     2014-12-12 00:24:48AM EOL: 0, UTF: 2

    ---------------------------------------------------------------------------------------------------------

    Below is my script. 

    Const defaultHostname = "ftp.Demo.net" 
    Const defaultPort = 21 
    Const defaultUsername = "Test" 
    Const defaultPassword = "abc1234$" 
    Const defaultRemoteDir = "/Test_Data1"

    Set fso  = CreateObject("Scripting.FileSystemObject")
    Set Shell  = CreateObject("Wscript.Shell")

    shell.Run "%comspec% /c FTP -v -s:" & scriptFile & " >> " & outputFile



    Set oExec = Shell.Exec(shellstring)
    Set logfile = fso.OpenTextFile(logfilename, 8, True)
    logfile.WriteLine "---------------------------File upload process started---------------------------------------"

    Set f = fso.OpenTextFile(logfilename)
    On Error Resume Next 
    Do While Not f.AtEndOfStream 
    WScript.Sleep (100)
        strline = f.ReadLine
        WScript.Echo Now & " : " & strline 'f.ReadLine
        LogFile.Writeline Now & " : " & f.ReadLine

    Loop

    logfile.Close
    f.Close
    Set fso  = Nothing
    Set Shell = Nothing
    Set logfile = Nothing 


    PT

    Saturday, December 13, 2014 1:47 PM
  • Why work so hard.  Try this.

    Function LogMsg(msg)
       dt=Replace(Now,"/","-")
       WScript.Echo "[" & dt & "]" & msg
    End Function

    WScript.Echo _
        Year(Now) & "-" _
        & Right("0" & Month(Now) ,2) & "-" _
        & Right("0" & Day(Now)   ,2) & " " _
        & Right("0" & Hour(Now)  ,2) & ":" _
        & Right("0" & Minute(Now),2) & ":" _
        & Right("0" & Second(Now),2)


    ¯\_(ツ)_/¯





    ¯\_(ツ)_/¯

    Saturday, December 13, 2014 2:03 PM
  • Thanks for your reply but its writing timestamp only at first line of log. 

    [13-Dec-14 7:52:06 PM]---------------------------File upload process started---------------------------------------

    How can we add timestamp as above before every line in the output file.



    PT

    Saturday, December 13, 2014 2:26 PM
  • You have to call that every time you want to write a timestamp.  I recommend using a function.

    Her is a place to start learning how to use VBScript: http://technet.microsoft.com/en-us/scriptcenter/dd772284

    Everything you need is on that page.


    ¯\_(ツ)_/¯

    Saturday, December 13, 2014 2:29 PM