CMD Application qtp/winshell/cmd interaction

Source: Internet
Author: User

===================================================================
' Using Windows.shell's sendkeys approach:
Set Objshell=createobject ("Wscript.Shell")
Objshell.run ("cmd")
Objshell. SendKeys ("{ENTER}")
Objshell. SendKeys ("CD C:\workpace\jenkins\testces\license")
Objshell. SendKeys ("{ENTER}")
Objshell. SendKeys ("Ant")
Objshell. SendKeys ("{ENTER}")
Objshell. SendKeys ("Exit")


===================================================================
' Run using the Wscrip.shell method:
CreateObject ("Wscript.Shell"). Run ("Taskkill/fi" imagename eq fm* "/F")
CreateObject ("Wscript.Shell"). Run ("Taskkill/fi" imagename eq mysqlserver.exe "/F")




' QTP run cmd, using a descriptive programming object operation mode:
Systemutil.run "cmd.exe", "", "C:\WINDOWS\system32", "open"
' Enter ping 51testing.com field
Window ("Object Class:=consolewindowclass"). Type "Ping baidu.com"
' Enter return
Window ("Object Class:=consolewindowclass"). Type Micreturn
' Get cmd inside the content
txt = window ("Object Class:=consolewindowclass"). Getvisibletext ()
' Print out the contents of cmd in log
Print txt


===================================================================
' EXEC mode using WshShell:
' Ping operation, and get Echo
Set Objshell=createobject ("Wscript.Shell")
Set exec = Objshell. Exec ("%comspec%/C ping baidu.com")
Print exec. Stdout.readall


' Delete all files with the suffix dat under the path
Set Objshell=createobject ("Wscript.Shell")
Set exec = Objshell. Exec ("%comspec%/C del c:\users\administrator\downloads\*.dat")
Wait 5


========================exec, run detailed ===============================
Method declaration:
Function Exec (ByVal Command as String) as Wshexec
Function Run (ByVal Command as String, [ByVal WindowStyle], [ByVal Waitonreturn]) as Integer
Difference:

1, return value
The return value of Run is an integer, which is 0 or 1 success and failure two states, and the return value of the Exec method is an object, from which you can get console output information and console error messages, such as stdout and stderr properties.
Such as:
Dim oshell,exers
Set Oshell = CreateObject ("Wscript.Shell")
CommandLine = "Ping baidu.com"
Set exers = oshell.exec (commandLine)
ErrMsg = ExeRs.StdErr.ReadAll ()
Stdmsg = ExeRs.StdOut.ReadAll ()
WScript.Echo "ErrMsg:" & ErrMsg & "Stdmsg:" & stdmsg


ret = Oshell.run (commandLine, 0, True)
WScript.Echo "Run Method return Value:" & RET
Set Oshell = Nothing
Set exers = Nothing
You can take the console error and console information via the control console.

PostScript: The Wshexec class has a property exitcode,processid,status,stderr,stdin,stdout and a function terminate, which are well understood.
The Status property has a value of three:
Const wshfailed = 2
Const wshfinished = 1
Const wshrunning = 0

2, execution parameters
The last two arguments of run, one is the style of the cmd window, and the other is whether to wait for execution to complete. The last parameter is useful if you want to wait for the execution of this cmd after the end of the program, then execute the statement after run, set this parameter to true, otherwise the statement will not wait for the cmd window to complete, run directly.
In addition, if you use the Exec method, if you want to wait for the CMD in the program after the execution, and then execute the following statement, you can also go through: oExec.StdErr.ReadAll () or OExec.StdOut.ReadAll (), the rationale should also be good to understand, to get the output of information, it must be executed after the CMD will be output.

Parameters of the Wshshell.run function
Set wshshell= CreateObject ("Wscript.Shell")
Wshshell.run "notepad.exe",
The run function has three parameters:
The first parameter is the path of the program you want to execute, if there are spaces in the path, remember to use "" in the form of "" "," "" "C:\Program Files\nn.exe" "or Chr &" C:\Program Files\nn.exe " The form of &CHR (34).
The second program is the form of a window, 0 is running in the background, 1 means normal operation, 2 means activating the program and showing it as minimized; 3 means activating the program and showing it as maximized; a total of 10 such parameters are shown in the following table.
The third argument is whether the script waits or resumes execution, and if set to true, the script waits for the calling program to exit and then execute backwards.
In fact, run as a function, preceded by a variable to accept the return value, in general, if the return is 0, indicating successful execution, if not 0, then the return value is the error code, you can find the corresponding error code.

Intwindowstyle description
0 hides a window and activates another window.
1 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. The application should specify this flag the first time the window is displayed.
2 Activates the window and displays it as a minimized window.
3 Activates the window and displays it as a maximized window.
4 Displays the window at the nearest window size and position. The active window remains active.
5 Activates the window and displays it at its current size and position.
6 minimizes the specified window and activates the Next top window in Z-order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window as the current state. The active window remains active.
9 activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. When you restore a minimized window, the application should specify this flag.
10 sets the display state according to the program state of the startup application.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CMD Application qtp/winshell/cmd interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.