No announcement yet.

Redirect script output to log file

  • Filter
  • Time
  • Show
Clear All
new posts

  • Redirect script output to log file

    I need redirect output of following script into log file. The script bellows retrieve WAN IP address using IE:

    Option Explicit
    Dim dtmStart, lngSeconds
    dtmStart = Now
    WScript.Echo "InternetExplorer.Application    " _
               & MyIP_IE( )      & "    (" _
               & DateDiff( "s", dtmStart, Now ) & " seconds)"
    Function MyIP_IE( )
    ' Name:       MyIP_IE
    ' Function:   Display your WAN IP address using Internet Explorer
    ' Usage:      ret = MyIP_IE( )
    ' Returns:    WAN (or global) IP address
    ' This script uses's automation page
    ' Script Source
        Dim blnTimedOut, i, objIE, objMatch, objRE, strText, strURL
        ' Return value if IP address couldn't be retrieved
        MyIP_IE = ""
        ' Open the appropriate URL in Internet Explorer
        strURL = ""
        Set objIE = CreateObject( "InternetExplorer.Application" )
        objIE.Visible = False
        objIE.Navigate2 strURL
        ' Wait till IE is ready
        i = 0
        blnTimedOut = False
        Do While objIE.Busy
            WScript.Sleep 100
            i = i + 1
            ' Time out after 10 seconds
            If i > 100 Then
                blnTimedOut = True
                Exit Do
            End If
        ' Retrieve the URL's text
        If Not blnTimedOut Then MyIP_IE = objIE.Document.Body.InnerText
        ' Close the Internet Explorer session
        Set objIE = Nothing
    End Function
    I finnd similar thread: where Rems advice to run the script from the command line (with cscript) and redirect the output to a text file. I've created shortcut, edited his properties as following:

    %comspec% /c "%windir%\system32\cscript.exe" //NoLogo "%userprofile%\Desktop\WANIP.vbs" >"%userprofile%\Desktop\WANIP.log"
    But whenever I'm running this shortcut it create an empty WANIP.log file on the desktop (I'm running it on Windows 7 computer with elevated privileges).
    By the same token, script itself working normally,- it return echo message with my current WAN IP address. So, why it doesn't redirect script output into the log file?

  • #2
    Re: Redirect script output to log file

    Originally posted by igor7 View Post
    I've created shortcut, edited his properties as following:

    %comspec% /c "%windir%\system32\cscript.exe" //NoLogo "%userprofile%\Desktop\WANIP.vbs" >"%userprofile%\Desktop\WANIP.log"
    The difference between your command line and the one in the example is that cmd /c in your script is directly followed by a double quote. A behavior is if the first character after cmd /c (or cmd /k) is a quote character and there are more than two quote signs used in the complete command line, the leading character and the last quote character will be removed, preserving any text after the last quote character. This behavior causes the command line will be start like this:
    %windir%\system32\cscript.exe" //NoLogo "%userprofile%\Desktop\WANIP.vbs" >"%userprofile%\Desktop\WANIP.log

    The line should therefore be like:
    %comspec% /c ""%windir%\system32\cscript.exe" //NoLogo "%userprofile%\Desktop\WANIP.vbs" >"%userprofile%\Desktop\WANIP.log""
    OR like,
    %comspec% /c >"%userprofile%\Desktop\WANIP.log" "%windir%\system32\cscript.exe" //NoLogo "%userprofile%\Desktop\WANIP.vbs"
    (I've edited the post you referred to, thanks)

    Last edited by Rems; 7th December 2011, 00:14.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts


    • #3
      Re: Redirect script output to log file

      Thank you for excellent explanation!! Now it work perfect!!