WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DOS

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get results from STDOUT - UDF

Keywords: 	STDOUT pipe piping data > >>

Instead of piping (pipe) ouput data to a file from the dos command line, this code can grab the data directly into WinBatch.

References:

#DefineFunction udfCaptureDosOutput( DosCommand )
   DebugTrace(22, ''); Allow DebugTrace continuation (inherit the debug mode from the caller).
   DataArray=ArrDimension(4)               ;Allocate return array
   ArrInitialize(DataArray,"")             ;initialize to all null strings
   ;Stuff the cmd in the 4 element of the array
   DataArray[3] = DosCommand
   oShell = ObjectCreate("WScript.Shell")    ;open shell object

   ;If 64 bit turn off redirection?
   If WinMetrics(-7) == 2
      ;64-bit Windows
      oldvalue = IntControl( 92, "disable", 0, 0, 0 )
      oScriptExec = oShell.Exec(DosCommand)   ;run the command
      IntControl( 92, "revert", oldvalue, 0, 0 )
   Else
      oScriptExec = oShell.Exec(DosCommand)   ;run the command
   EndIf

   ;Open output objects
   oStdOut = oScriptExec.StdOut
   oStdErr = oScriptExec.StdErr

   While (oScriptExec.Status==0)           ;wait for completion

      ;Caputure StdOut data
      oStdOut = oScriptExec.StdOut
      While ! oStdOut.AtEndOfStream
         strLine = oStdOut.ReadLine
         DataArray[1] = StrCat(DataArray[1],strLine,@CRLF)
      EndWhile

      ;Capture StdErr data
      oStdErr = oScriptExec.StdErr
      While ! oStdErr.AtEndOfStream
         strLine = oStdErr.ReadLine
         DataArray[2] = StrCat(DataArray[2],strLine,@CRLF)
      EndWhile

      TimeDelay(0.1)
   EndWhile

   ;Get remainder of data, if any

      ;Caputure StdOut data
      oStdOut = oScriptExec.StdOut
      While ! oStdOut.AtEndOfStream
         strLine = oStdOut.ReadLine
         DataArray[1] = StrCat(DataArray[1],strLine,@CRLF)
      EndWhile

      ;Capture StdErr data
      oStdErr = oScriptExec.StdErr
      While ! oStdErr.AtEndOfStream
         strLine = oStdErr.ReadLine
         DataArray[2] = StrCat(DataArray[2],strLine,@CRLF)
      EndWhile


   DataArray[0]=oScriptExec.ExitCode         ;save errorlevel/exit code

   ;Close handles that were opened
   oStdOut = 0
   oStdErr = 0
   oScriptExec = 0
   oShell = 0

   ;Return the array
   Return(DataArray)

#EndFunction

#DefineFunction udfDisplayResult( dataarray )
   Exitcode = dataarray[0]
   StdOut = dataarray[1]
   StdErr = dataarray[2]
   DosCommand = dataarray[3]
   If stdErr != ""
      Message(DosCommand: " - STDERR", StdErr)
      Message(DosCommand:" - EXITCODE", Exitcode)
   Else
      Message(DosCommand:" - STDOUT", StdOut)
   EndIf
   Return 1
#EndFunction



;Vol
;Displays the disk volume label and serial number, if they exist. A serial number is displayed for a disk
;formatted with MS-DOS version 4.0 or later.
cmd = 'cmd /c vol c:'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Ver
;Displays the Windows XP version number.
cmd = 'cmd /c ver'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Tree
;Graphically displays the directory structure of a path or of the disk in a drive.
DirChange(DirWindows(0))
cmd = 'cmd /c tree /a'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Tracert
;Determines the path taken to a destination by sending Internet Control Message Protocol (ICMP) Echo Request
;messages to the destination with incrementally increasing Time to Live (TTL) field values. The path displayed
;is the list of near-side router interfaces of the routers in the path between a source host and a destination.
;The near-side interface is the interface of the router that is closest to the sending host in the path.
cmd = 'tracert www.winbatch.com'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )



;Tasklist
;Displays a list of applications and services with their Process ID (PID) for all tasks running on either a local or a remote computer.
cmd = 'Tasklist'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )



;Systeminfo
;Displays detailed configuration information about a computer and its operating system, including operating
;system configuration, security information, product ID, and hardware properties, such as RAM, disk space, and network cards.
cmd = 'systeminfo'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Schtasks
;Schedules commands and programs to run periodically or at a specific time. Adds and removes tasks from the schedule,
;starts and stops tasks on demand, and displays and changes scheduled tasks.
;Displays all tasks scheduled to run on the computer, including those scheduled by other users.
cmd = 'Schtasks'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Route
;Displays and modifies the entries in the local IP routing table.
;To display the entire contents of the IP routing table, type:
cmd = 'route print'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Prnport.vbs
;Creates, deletes, and lists standard TCP/IP printer ports, in addition to displaying and changing port configuration.
DirChange("C:\WINDOWS\system32\")
cmd = '"cscript" "Prnport.vbs" -l'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Prnmngr.vbs
;Adds, deletes, and lists printers or printer connections, in addition to setting and displaying the default printer.
;Used without parameters, prnmngr.vbs displays command-line help for the prnmngr.vbs command.
;To list all of the printers for a computer
DirChange("C:\WINDOWS\system32\")
cmd = '"cscript" "prnmngr.vbs" -l'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Prnjobs.vbs
;Pauses, resumes, cancels, and lists print jobs.
;To list the print jobs in a print queue
DirChange("C:\WINDOWS\system32\")
cmd = '"cscript" "prnjobs.vbs" -l'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )

;Prndrvr.vbs
;Adds, deletes, and lists printer drivers.
;To list the printer drivers on a computer
DirChange("C:\WINDOWS\system32\")
cmd = '"cscript" "prndrvr.vbs" -l'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Prncnfg.vbs
;Configures or displays configuration information about a printer. Used without parameters, prncnfg.vbs
;displays command-line help for the prncnfg.vbs command.
;To display configuration information about a printer
If FileLocate("WWPRT34i.DLL") != ""
   AddExtender("WWPRT34i.DLL")
   defprt = pGetDefPrtInf(1)
   DirChange("C:\WINDOWS\system32\")
   cmd = StrCat('"cscript" "prncnfg.vbs" -g -p "',defprt,'"')
   dataarray = udfCaptureDosOutput( cmd )
   udfDisplayResult( dataarray )
EndIf


;Nslookup
;Displays information that you can use to diagnose Domain Name System (DNS) infrastructure. Before using
;this tool, you should be familiar with how DNS works. The Nslookup command-line tool is available only if
;you have installed the TCP/IP protocol.
cmd = "Nslookup ls"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Netstat
;Displays active TCP connections, ports on which the computer is listening, Ethernet statistics,
;the IP routing table, IPv4 statistics (for the IP, ICMP, TCP, and UDP protocols), and IPv6 statistics
;(for the IPv6, ICMPv6, TCP over IPv6, and UDP over IPv6 protocols). Used without parameters, netstat
;displays active TCP connections.

;Displays all active TCP connections and the TCP and UDP ports on which the computer is listening.
cmd = "Netstat -a"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )

;Displays Ethernet statistics, such as the number of bytes and packets sent and received.
cmd = "Netstat -e"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Displays active TCP connections, however, addresses and port numbers are expressed numerically and
;no attempt is made to determine names.
cmd = "Netstat -n"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )

;Displays the contents of the IP routing table. This is equivalent to the route print command.
cmd = "Netstat -r"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )



;Ipxroute
;To display the network segments that the workstation is attached to, the workstation node address,
;and frame type being used
cmd = "ipxroute config"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Net statistics
;Displays the statistics log for the local Workstation or Server service, or the running services for which
;statistics are available. Used without parameters, net statistics lists the running services for which
;statistics are available.
cmd = "cmd.exe /c net statistics workstation"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )

;Date
;Displays the current system date setting. Used without parameters, date displays the current system date
;setting and prompts you to type a new date.
cmd = "cmd.exe /c date /t"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Net view
;Displays a list of domains, computers, or resources that are being shared by the specified computer.
;Used without parameters, net view displays a list of computers in your current domain.
cmd = "cmd.exe /c net view"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Ipconfig
;Displays all current TCP/IP network configuration values and refreshes Dynamic Host Configuration Protocol
;(DHCP) and Domain Name System (DNS) settings. Used without parameters, ipconfig displays the IP address,
;subnet mask, and default gateway for all adapters.
cmd = "ipconfig /all"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;nbtstat
;To display the NetBIOS name table of the local computer
cmd = 'nbtstat -n'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Getmac
;Returns the media access control (MAC) address and list of network protocols associated with each address for all network
;cards in each computer, either locally or across a network.
cmd = 'Getmac'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


;Eventquery
;Lists the events and event properties from one or more event logs.
DirChange("C:\WINDOWS\system32\")
cmd = '"cscript.exe" eventquery.vbs /l application'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )

;Driverquery
;Displays a list of all installed device drivers and their properties.
cmd = "driverquery"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )

;Ping
;Verifies IP-level connectivity to another TCP/IP computer by sending Internet Control Message Protocol (ICMP)
;Echo Request messages. The receipt of corresponding Echo Reply messages are displayed, along with round-trip times.
;Ping is the primary TCP/IP command used to troubleshoot connectivity, reachability, and name resolution. Used
;without parameters, ping displays help.
cmd = "ping www.winbatch.com"
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )

;FC - File Compare
;To make an ASCII comparison of two text files that are named Monthly.rpt and Sales.rpt and display the results in abbreviated format,
;  fc /a monthly.rpt sales.rpt
;To make a binary comparison of two batch files named Profits.bat and Earnings.bat, type:
;  fc /b profits.bat earnings.bat
DirChange("C:\temp\data\")
cmd = 'fc /a C:\Temp\Data\test.txt C:\Temp\Data\test2.txt'
dataarray = udfCaptureDosOutput( cmd )
udfDisplayResult( dataarray )


Exit


Article ID:   W15939
File Created: 2010:05:13:12:13:08
Last Updated: 2010:05:13:12:13:08