Announcement

Collapse
No announcement yet.

Display a text file to the user.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Display a text file to the user.

    I'm writing the results of a script to a text file and would like to open the text file and display it to the user. What's the code for this? Thanks.
    |
    +-- JDMils
    |
    +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
    |

  • #2
    Re: Display a text file to the user.

    You would like a script to open a file??

    Just type in the path of the file...
    C:\Temp\temp.txt will open Temp.txt in notepad (or whatever the default application is)

    If you would like to open it with a set program, call the program, with the path of the file after it. Like
    %programfiles%\notepad.exe C:\temp\temp.txt

    You can also use the start command
    C:\Start readme.txt Will open the file Readme.txt at the root of C (or where this is called from)
    C:\Start C:\temp\temp.txt will open the temp.txt file in C:\temp.

    http://frank.neatstep.com/node/84 <-- Standard basics
    http://www.davehylands.com/Software/Open/ <-- 3rd party app (I have NOT test this app, but used other like them. I see no point in them).

    Anyhow, hope this helped.

    Wofen
    Good to be back....

    Comment


    • #3
      Re: Display a text file to the user.

      The normal syntax is "notepad filename" where filename is the full name (including path)
      Avoid spaces in the name and path or wrap it in double quotes
      Tom Jones
      MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
      PhD, MSc, FIAP, MIITT
      IT Trainer / Consultant
      Ossian Ltd
      Scotland

      ** Remember to give credit where credit is due and leave reputation points where appropriate **

      Comment


      • #4
        Re: Display a text file to the user.

        Originally posted by Ossian View Post
        The normal syntax is "notepad filename" where filename is the full name (including path)
        Avoid spaces in the name and path or wrap it in double quotes

        Opps, yeah, sry, add some " ", esp if the filenames have spaces.

        Wofen
        Good to be back....

        Comment


        • #5
          Re: Display a text file to the user.

          Thanks guys. Here's the final code which works perfectly:
          Code:
          '==========================================================================
          '
          ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2007
          '
          ' NAME: List MSIE Proxy Settings
          '
          ' AUTHOR: Julian Milano
          ' DATE  : 28/10/2010
          '
          ' COMMENT: 
          '
          ' Enumerates proxy settings for current user and displays the details In
          ' a text file.
          '==========================================================================
          
          
          DIM FSO
          
          ' Report FileName:
          fFileName = "Results.txt"
          ' Choose current folder for report home:
          CurDir = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
          
          ' Create the text file.
          Set fso = CreateObject("Scripting.FileSystemObject")
          Set fResultFile = fso.CreateTextFile(CurDir & fFileName, True)
          
          strComputer = "."
          Set objWMIService = GetObject("winmgmts:\\" & strComputer _
              & "\root\cimv2\Applications\MicrosoftIE")
          ' Get MSIE settings from WMI.
          Set colIESettings = objWMIService.ExecQuery _
              ("Select * from MicrosoftIE_LANSettings")
          For Each strIESetting in colIESettings
              fResultFile.writeline "Autoconfiguration proxy: " & strIESetting.AutoConfigProxy
              fResultFile.writeline  "Autoconfiguration URL: " & strIESetting.AutoConfigURL
              fResultFile.writeline  "Autoconfiguration Proxy detection mode: " & _
                  strIESetting.AutoProxyDetectMode
              fResultFile.writeline  "Proxy: " & strIESetting.Proxy
              fResultFile.writeline  "Proxy override: " & strIESetting.ProxyOverride
              fResultFile.writeline  "Proxy server: " & strIESetting.ProxyServer
              fResultFile.close
          Next
          
          ' Display the results in notepad.
          Set wShell = WScript.CreateObject("WScript.Shell")
          wShell.run "Notepad.exe " & CurDir & fFileName 
          Set wShell = Nothing
          
          WScript.Echo "Script Finished."
          |
          +-- JDMils
          |
          +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
          |

          Comment


          • #6
            Re: Display a text file to the user.

            Thank you for positng the finished script.

            Wofen
            Good to be back....

            Comment


            • #7
              Re: Display a text file to the user.

              Originally posted by JDMils View Post
              Code:
              For Each strIESetting in colIESettings
                  fResultFile.writeline "Autoconfiguration proxy: " & strIESetting.AutoConfigProxy
                         '    2
                         '    3
                         '    4
                         '    5
                         '    6
                  fResultFile.close
              Next
              Technically fResultFile.close should be placed after the For-Each loop.



              Originally posted by Wofen View Post
              add some " ", esp if the filenames have spaces.
              In vbscript use doubled double quotes to wrap a file name and path in quotes.


              Code:
              ' *cut* ----------8< - - - - - - -
              
              
              For Each strIESetting in colIESettings
                 fResultFile.writeline "Autoconfiguration proxy" & space(15) _
                                & ": " & strIESetting.AutoConfigProxy
                 fResultFile.writeline  "Autoconfiguration URL" & space(17) _
                                & ": " & strIESetting.AutoConfigURL
                 fResultFile.writeline  "Autoconfiguration Proxy detection mode" _
                                & ": " & strIESetting.AutoProxyDetectMode
                 fResultFile.writeline  "Proxy" & space(33) _
                                & ": " & strIESetting.Proxy
                 fResultFile.writeline  "Proxy override" & space(24) _
                                & ": " & strIESetting.ProxyOverride
                 fResultFile.writeline  "Proxy server" & space(26) _
                                & ": " & strIESetting.ProxyServer
              Next
              
              fResultFile.close
              
              ' Display the results in notepad.
              Set wShell = WScript.CreateObject("WScript.Shell")
              wShell.run "Notepad.exe """ & CurDir & fFileName & """"
              Set wShell = Nothing
              \Rems

              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

              Comment


              • #8
                Re: Display a text file to the user.

                Thanks REMs for the cosmetic enhancements! I also moved the Close method and added the quotes to the Notepad command.

                New code list is:
                Code:
                '==========================================================================
                '
                ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2007
                '
                ' NAME: List MSIE Proxy Settings
                '
                ' AUTHOR: Julian Milano
                ' DATE  : 28/10/2010
                '
                ' COMMENT: 
                '
                ' Enumerates proxy settings for current user and displays the details In
                ' a text file.
                '==========================================================================
                
                
                DIM FSO
                
                ' Report FileName:
                fFileName = "Results.txt"
                ' Choose current folder for report home:
                CurDir = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
                
                ' Create the text file.
                Set fso = CreateObject("Scripting.FileSystemObject")
                Set fResultFile = fso.CreateTextFile(CurDir & fFileName, True)
                
                strComputer = "."
                Set objWMIService = GetObject("winmgmts:\\" & strComputer _
                    & "\root\cimv2\Applications\MicrosoftIE")
                ' Get MSIE settings from WMI.
                Set colIESettings = objWMIService.ExecQuery _
                    ("Select * from MicrosoftIE_LANSettings")
                For Each strIESetting in colIESettings
                   fResultFile.writeline "Autoconfiguration proxy" & space(17) & _
                   							": " & strIESetting.AutoConfigProxy
                   fResultFile.writeline  "Autoconfiguration URL" & space(19) & _
                   							": " & strIESetting.AutoConfigURL
                   fResultFile.writeline  "Autoconfiguration Proxy detection mode  " & _
                   							": " & strIESetting.AutoProxyDetectMode
                   fResultFile.writeline  "Proxy" & space(35) & _
                   							": " & strIESetting.Proxy
                   fResultFile.writeline  "Proxy server" & space(28) & _
                   							": " & strIESetting.ProxyServer
                   fResultFile.writeline  "Proxy override" & space(26) & _
                   							": " & strIESetting.ProxyOverride
                Next
                
                fResultFile.close
                
                ' Display the results in notepad.
                Set wShell = WScript.CreateObject("WScript.Shell")
                wShell.run "Notepad.exe """ & CurDir & fFileName & """" 
                Set wShell = Nothing
                
                WScript.Echo "Script Finished."
                Last edited by JDMils; 29th October 2010, 02:30.
                |
                +-- JDMils
                |
                +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
                |

                Comment


                • #9
                  Re: Display a text file to the user.

                  You might want to destroy the rest of your "Set" commands as well (IE: Set fso = Nothing).

                  Is the goal is to give them a screen they can edit?

                  If you just want to show them the information you can build a string and show them in a popup (no edit box) or if the goal is for them to print you can add a "/P" to the notepad command (Prints the current page to default printer and closes notepad).

                  Hobie

                  Comment


                  • #10
                    Re: Display a text file to the user.

                    Hobie,

                    The goal is to display the information to the user while giving them the ability to copy the results to another application.
                    |
                    +-- JDMils
                    |
                    +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
                    |

                    Comment


                    • #11
                      Re: Display a text file to the user.

                      Originally posted by JDMils View Post
                      Hobie,

                      The goal is to display the information to the user while giving them the ability to copy the results to another application.
                      If the file does not necessarily need to be saved, you could instead invoke a popup window that is showing the results. The popup windows from the sample below alows copying text from it.
                      Code:
                      Dim objWMIService, colIESettings, strIESetting, fResult
                      
                      Set objWMIService = GetObject("winmgmts:" _
                          & "root\cimv2\Applications\MicrosoftIE")
                      ' Get MSIE settings from WMI.
                      Set colIESettings = objWMIService.ExecQuery _
                          ("Select * from MicrosoftIE_LANSettings")
                      
                      For Each strIESetting in colIESettings
                         fResult = fResult & "Autoconfiguration proxy" & space(15) _
                                        & ": " & strIESetting.AutoConfigProxy & vbNewLine
                         fResult = fResult & "Autoconfiguration URL" & space(17) _
                                        & ": " & strIESetting.AutoConfigURL & vbNewLine
                         fResult = fResult & "Autoconfiguration Proxy detection mode" _
                                        & ": " & strIESetting.AutoProxyDetectMode & vbNewLine
                         fResult = fResult & "Proxy" & space(33) _
                                        & ": " & strIESetting.Proxy & vbNewLine
                         fResult = fResult & "Proxy override" & space(24) _
                                        & ": " & strIESetting.ProxyOverride & vbNewLine
                         fResult = fResult &   "Proxy server" & space(26) _
                                        & ": " & strIESetting.ProxyServer & vbNewLine
                      Next
                      
                      '------------------Result File / Notepad -------------------------------
                      Dim fFileName, CurDir, fso, fResultFile
                      ' Report FileName:
                      fFileName = "Results.txt"
                      ' Choose current folder for report home:
                      CurDir = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
                      
                      ' Create the text file.
                      Set fso = CreateObject("Scripting.FileSystemObject")
                      Set fResultFile = fso.CreateTextFile(CurDir & fFileName, True)
                      fResultFile.write fResult
                      fResultFile.close
                      
                      ' Display the results in notepad.
                      WScript.CreateObject("WScript.Shell") _
                         .run "Notepad.exe """ & CurDir & fFileName & """"
                      '-----------------------------------------------------------------------
                      
                      PopupWindow fResult
                      
                      Wscript.Quit
                      
                      Sub PopupWindow (byval strBody)
                         Dim sWidth, sHeight
                         sWidth = 600 : sHeight = 230
                      
                         ' Convert input string to a valid HTML string
                         ' http://rabbit.eng.miami.edu/info/htmlchars.html
                         strBody = Replace(strBody,"&","&amp;",1,-1,vbTextCompare)
                         strBody = Replace(strBody,Chr(34),"&quot;",1,-1,vbTextCompare)
                         strBody = Replace(strBody," ","&nbsp;",1,-1,vbTextCompare)
                         strBody = Replace(strBody,">","&gt;",1,-1,vbTextCompare)
                         strBody = Replace(strBody,"<","&lt;",1,-1,vbTextCompare)
                         strBody = Replace(strBody, vbNewLine, "<br>")
                      
                         with HTABox(sHeight, sWidth)
                           .document.title = "Results - IE LAN Settings"
                           .msg.innerHTML = "<P Align=Left><Font size='2' Face" _
                                       & "='Courier New'>" & strBody & "</P><P>" _
                                       & "<br></P><P><input type=submit value=" _
                                       & "Close onclick='done.value=true'></P>"
                           do until .done.value : wsh.sleep 50 : loop
                           .close
                         end with
                         CreateObject("WScript.Shell").SendKeys "^"
                      End Sub
                      
                      Function HTABox(h, w)
                         ' # HTA based message box
                         ' # Jun 25 2010 / Tom Lavedas
                         Dim l, t, nRnd, IE, sCmd
                         With createobject("internetexplorer.application")
                           l = .width
                           t = .height
                           .quit
                         End with
                      
                         nRnd = hex(Timer*100)
                         sCmd = "mshta.exe ""javascript:{new " _ 
                             & "ActiveXObject(""InternetExplorer.Application"")" _ 
                             & ".PutProperty('" & nRnd & "',window);window" _
                             & ".resizeTo();window.moveTo(" & Round(l/2, 0) & "," _
                             & Round(t/2, 0) & ")}"""
                      
                         With CreateObject("WScript.Shell") 
                           .Run sCmd, 2, False
                           do until .AppActivate("javascript:{new ") : WSH.sleep 10 : loop
                         End With ' WSHShell 
                      
                         For Each IE In CreateObject("Shell.Application").windows 
                           If IsObject(IE.GetProperty(nRnd)) Then 
                             set HTABox = IE.GetProperty(nRnd)
                             IE.quit
                             HTABox.document.title = "HTABox" 
                             HTABox.document.write _ 
                               "<HTA:Application contextMenu=no border=thin " _ 
                               & "minimizebutton=no maximizebutton=no sysmenu=no />" _ 
                               & "<body scroll=no style=filter:progid:DXImageTrans" _
                               & "form.Microsoft.Gradient(GradientType=0,StartColor" _
                               & "Str='#86cceb',endColorStr='#5589ab');font:normal " _
                               & "10pt Arial' onbeforeunload='vbscript:if not done." _ 
                               & "value then window.event.cancelBubble=true:" _ 
                               & "window.event.returnValue=false:" _ 
                               & "done.value=true:end if'>" _ 
                               & "<input type=hidden id=done value=false>" _ 
                               & "<center><span id=msg>&nbsp;</span><center></body>"
                             l = Round( (l-w)/2, 0)
                             t = Round( (t-h)/2, 0)
                             HTABox.moveTo l,t
                             HTABox.resizeTo w,h
                             Exit Function 
                           End If 
                         Next 
                         ' I can't imagine how this line can be reached, but just in case
                         MsgBox "HTA window not found." :  wsh.quit
                      End Function
                      The orange block "Result File / Notepad" can be removed from the script.

                      \Rems

                      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

                      Comment


                      • #12
                        Re: Display a text file to the user.

                        Had a problem with the code. Had to change:
                        Code:
                        do until .AppActivate("javascript:{new ") : WSH.sleep 10 : loop
                        to
                        Code:
                        Do Until .AppActivate("javascript:{new ") : .sleep 10 : Loop
                        Also, on this line:
                        Code:
                        If IsObject(IE.GetProperty(nRnd)) Then
                        I get the error:
                        Untitled2.vbs(94): Exception: Object doesn't support this property or method: 'IE.GetProperty'
                        |
                        +-- JDMils
                        |
                        +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
                        |

                        Comment


                        • #13
                          Re: Display a text file to the user.

                          Originally posted by JDMils View Post
                          Had a problem with the code. Had to change:
                          Code:
                          do until .AppActivate("javascript:{new ") : WSH.sleep 10 : loop
                          to
                          Code:
                          Do Until .AppActivate("javascript:{new ") : .sleep 10 : Loop
                          Your change is incorrect and must be raising an error for sure! Because Sleep is not a method of "WScript.Shell".
                          It should be wscript.sleep (or wsh.sleep), it is a windows script host buildin function.
                          What error you've got there with my script,, and... how come that you don't get any error there now after your edit, have you made any other changes?

                          _
                          Originally posted by JDMils View Post
                          Also, on this line:
                          Code:
                          If IsObject(IE.GetProperty(nRnd)) Then
                          I get the error:
                          Untitled2.vbs(94): Exception: Object doesn't support this property or method: 'IE.GetProperty'
                          What is the OS and SP and the version of IE is on the computer?
                          / try adding Error trapping:
                          Code:
                             On Error Resume Next
                             For Each IE In CreateObject("Shell.Application").windows()
                               If IsObject(IE.GetProperty(nRnd)) Then
                                 Set HTABox = IE.GetProperty(nRnd): IE.quit
                                 HTABox.document.title = "HTABox"
                                 HTABox.document.write _
                                   "<HTA:Application contextMenu=no border=thin " _
                                   & "minimizebutton=no maximizebutton=no sysmenu=no />" _
                                   & "<body scroll=no style=filter:progid:DXImageTrans" _
                                   & "form.Microsoft.Gradient(GradientType=0,StartColor" _
                                   & "Str='#86cceb',endColorStr='#5589ab');font:normal " _
                                   & "10pt Arial' onbeforeunload='vbscript:if not done." _
                                   & "value then window.event.cancelBubble=true:" _
                                   & "window.event.returnValue=false:" _
                                   & "done.value=true:end if'>" _
                                   & "<input type=hidden id=done value=false>" _
                                   & "<center><span id=msg>&nbsp;</span><center></body>"
                                 l = Round( (l-w)/2, 0)
                                 t = Round( (t-h)/2, 0)
                                 HTABox.moveTo l,t
                                 HTABox.resizeTo w,h
                                 Exit Function
                               End If
                             Next
                             On Error Goto 0
                          The script as a whole should run on any Windows 2k/xp and server 2k/2k3 without problems (the name space "root\cimv2\Applications\MicrosoftIE" is no longer available on Vista/2k8 and newer. While the popup-window like it is in my previous post, is also supported on 2k8/r2 unless applications or js is blocked. It runs wel here.) Have you done any customizations to the script, that might be the cause of this?

                          If the popup window doesn't work in your environment, then continue using the notepad solution.


                          \Rems

                          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

                          Comment


                          • #14
                            Re: Display a text file to the user.

                            When I run your code, I get:
                            \Scripts & CMDs\Active Directory\AD Groups\Untitled3.vbs(90): Exception: Object required: 'wsh'
                            on line:
                            Code:
                            Do Until .AppActivate("javascript:{new ") : WSH.sleep 10 : Loop
                            With the error trap inserted as specified, I get:

                            \Scripts & CMDs\Active Directory\AD Groups\Untitled3.vbs(59): Exception: Object required: 'HTABox(...)'
                            on line:
                            Code:
                            With HTABox(sHeight, sWidth)
                            The above was using Primalscript 2007's internal engine and VS2010's internal engine to run the script.

                            OS is:
                            Host Name: WS03529
                            OS Name: Microsoft Windows XP Professional
                            OS Version: 5.1.2600 Service Pack 3 Build 2600
                            OS Manufacturer: Microsoft Corporation
                            OS Configuration: Member Workstation
                            OS Build Type: Multiprocessor Free
                            Registered Owner: MyCompany Pty Ltd
                            Registered Organization: MyCompany Pty Ltd
                            Product ID: 76487-642-1522515-23860
                            Original Install Date: 16/03/2010, 2:25:26 PM
                            System Up Time: 4 Days, 8 Hours, 27 Minutes, 19 Seconds
                            System Manufacturer: LENOVO
                            System Model: 6136A1M
                            System type: X86-based PC
                            Processor(s): 1 Processor(s) Installed.
                            [01]: x86 Family 6 Model 23 Stepping 10 GenuineIntel ~2992 Mhz
                            BIOS Version: LENOVO - 136
                            Windows Directory: C:\WINDOWS
                            System Directory: C:\WINDOWS\system32
                            Boot Device: \Device\HarddiskVolume1
                            System Locale: en-au;English (Australia)
                            Input Locale: en-us;English (United States)
                            Time Zone: (GMT+10:00) Canberra, Melbourne, Sydney
                            Total Physical Memory: 1,900 MB
                            Available Physical Memory: 386 MB
                            Virtual Memory: Max Size: 2,048 MB
                            Virtual Memory: Available: 1,997 MB
                            Virtual Memory: In Use: 51 MB
                            Page File Location(s): C:\pagefile.sys
                            |
                            +-- JDMils
                            |
                            +-- Regional Systems Engineer, DotNet programmer & Jack of all trades
                            |

                            Comment


                            • #15
                              Re: Display a text file to the user.

                              Originally posted by JDMils View Post
                              The above was using Primalscript 2007's internal engine and VS2010's internal engine to run the script.
                              The script is supported for sure by Microsoft Windows Script Host Version 5.7. and IE 8.

                              It runs well when executed using the usual wscript.exe or cscript.exe
                              It must be the engine you are using.


                              \Rems

                              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

                              Comment

                              Working...
                              X