Working with DOS Command Line
Posted by admin - Apr 16, 2008 Articles, Dani Vainstein 0 0 Views : 2343 Receive Updates For This Category
Article Tools
- Print this page
- Add Comment
- Send to Friend
- Last Updated on :
Jul 16, 2011
Objectives
The objectives of this article are :
- Learn how to use DOS command lines in QTP.
- Alternative complex tasks.
- Alternative .NET methods.
cmd.exe allows access to the Microsoft Windows Command Prompt, also known as Microsoft DOS.
To-date, cmd.exe is a 32-bit command prompt used in Windows NT, 2000, and XP and offers disk and file maintenance functions to your computer as well as network functions. cmd.exe can be found under System32 folder.
SystemUtil.Run "cmd.exe","","C:\WINDOWS\system32","open"
Capture the window by QTP
Window( "object class:=ConsoleWindowClass" )
Print Window( "object class:=ConsoleWindowClass" ).GetROProperty( "title" )
Typing to DOS window
The Type method types the specified string in the window.
Syntax: object.Type KeyboardInput.
KeyboardInput : A String value. The text string and/or constants representing non-alphanumeric keys.
Window( "object class:=ConsoleWindowClass" ).Type "Ping advancedqtp.com"
Window( "object class:=ConsoleWindowClass" ).Type micReturn
Catching the Response
The GetVisibleText method returns the text from the specified area. The text to capture must be visible in the application window when the step runs. The area is defined by pairs of coordinates that designate two diagonally opposite corners of a rectangle.
Syntax : object.GetVisibleText ([Left], [Top], [Right], [Bottom])
txt = Window( "object class:=ConsoleWindowClass" ).GetVisibleText()
print txt
Problems…
SystemUtil.Run "cmd.exe","","C:\WINDOWS\system32","open"
Window( "object class:=ConsoleWindowClass" ).Type "dir"
Window( "object class:=ConsoleWindowClass" ).Type micReturn
txt = Window( "object class:=ConsoleWindowClass" ).GetVisibleText()
print txt
The GetVisibleText method “catches” only the text that is visible. large commands with large responses are useless…
Solution
Wscript.Shell Object
The WSript.Shell object provides functions to read system information and environment variables, work with the registry and manage shortcuts. You create a WSript.Shell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.
Dim wShell
Set wShell = CreateObject( "WScript.Shell" )
you will see this detailed description if you are using PDM.DLL version 9
Exec Method
Runs an application in a child command-shell, providing access to the StdIn/StdOut/StdErr streams.
Syntax : object.Exec( strCommand )
strCommand : String value indicating the command line used to run the script. The command line should appear exactly as it would if you typed it at the command prompt.
The Exec method returns a WshScriptExec object, which provides status and error information about a script run with Exec along with access to the StdIn, StdOut, and StdErr channels.
%compsec%
What if your colleague installed MS-Windows on D:\ Drive? –> The C:\Windows\System32\cmd.exe will not work.
Since every machine can have different installation directory, the environment %compsec% returns the full path of the cmd.exe file. ![]()
Executing a command
- /C– Carries out the command specified by string and then terminates.
- /K – Carries out the command specified by string but remains.
Executing the command
Dim wShell, exec
Set wShell = CreateObject( "WScript.Shell" )
Set exec = wShell.Exec( "%comspec% /C ping advancedqtp.com" )
WshScriptExec Object
Provides status information about a script run with Exec along with access to the StdIn, StdOut, and StdErr streams.
WshScriptExec.Status Property
Provides status information about a script run with the Exec() method.
- WshRunning ( = 0 )- The job is still running.
- WshFinished ( = 1 ) – The job has completed.
WshScriptExec.ExitCode Property
Returns the exit code set by a script or program run using the Exec() method. Executables set an exit code when they finish running. This conveys the status information when a process ends. Often, it is used to send an error code (or some other piece of information) back to the caller. If the process has not finished, the ExitCode property returns 0. The values returned from ExitCode depend on the application that was called.
WshScriptExec.StdOut Property
The StdOut property contains a read-only copy of any information the script may have sent to the standard output.
- AtEndOfStream- Returns a Boolean value indicating whether the end of an input stream has been reached.
- ReadAll- Returns all characters from a stream.
- ReadLine – Reads an entire line from a stream.
Handling Errors
Run the following code. why is the result empty?
Dim wShell, exec
Set wShell = CreateObject( "WScript.Shell" )
Set exec = wShell.Exec( "%comspec% /C png advancedqtp.com" )
print exec.StdOut.ReadAll
WshScriptExec.StdErr Property
since the “png” command has a syntax error ( ping ), the STDOUT remains empty.
Now, run the following code :
Dim wShell, exec
Set wShell = CreateObject( "WScript.Shell" )
Set exec = wShell.Exec( "%comspec% /C png advancedqtp.com" )
print exec.StdErr.ReadAll
Since we have a syntax error, the STDERR buffer is filled, instead of the STDOUT. but, what about a “bad” command ( without syntax errors )?
Dim wShell, exec
Set wShell = CreateObject( "WScript.Shell" )
Set exec = wShell.Exec( "%comspec% /C ping no-exists" )
print exec.StdOut.ReadAll
in this case STDOUT will be filled, but not STDERR.
How could we know when an error occurred?
Dim wShell, exec, outStr
Set wShell = CreateObject( "WScript.Shell" )
Set exec = wShell.Exec( "%comspec% /C ping arrdvancedqtp.com" )
Do While exec.Status = 0
If Not exec.StdOut.AtEndOfStream Then
outStr = exec.StdOut.ReadAll
If exec.ExitCode = 1 then
Reporter.ReportEvent micWarning, "Command failed", outStr
End If
Exit Do
End If
If Not exec.StdErr.AtEndOfStream Then
outStr = "STDERR: " & exec.StdErr.ReadAll
Reporter.ReportEvent micFail, "Command failed", outStr
Exit Do
End If
Wait 1
Loop
Print outStr
Alternative .NET
Set nt = DotNetFactory.CreateInstance( "Microsoft.VisualBasic.Devices.Network" )
If CBool( nt.Ping( "www.advancedqtp.com" ) ) Then
MsgBox( "Server pinged successfully." )
Else
MsgBox( "Ping request timed out." )
End If
Demo
Print a list of “.exe” files under System32 without header information in lower case order by file name descending.
Option Explicit
Const SystemFolder = 1
Dim wShell, exec, fso
Dim dirList
Set wShell = CreateObject( "WScript.Shell" )
Set fso = CreateObject("Scripting.FileSystemObject")
dirList = fso.GetSpecialFolder( SystemFolder ) & "\*.exe"
Set exec = wShell.Exec( "%comspec% /C dir " & dirList & " /B /O-N /L" )
Do While True
If Not exec.StdOut.AtEndOfStream Then
dirList = exec.StdOut.ReadAll
If exec.ExitCode = 1 then
Reporter.ReportEvent micWarning, "Command failed", dirList
End If
Exit Do
End If
If Not exec.StdErr.AtEndOfStream Then
dirList = "STDERR: " & exec.StdErr.ReadAll
Reporter.ReportEvent micFail, "Command failed", dirList
Exit Do
End If
Wait 1
Loop
Print dirList


