Run and exec usage of wscript. Shell Objects in VBScript

Source: Internet
Author: User
Tags pscp

Http://blog.csdn.net/zmxj/archive/2009/02/25/3937372.aspx

The wscript. Shell object provides the run and exec methods. Recently, the project needs to use pscp In VBScript to copy files from the Linux server. The exec method is used. The following code

Set exers = objws. Exec ("pscp-r-SCP-pw
"& Password &" & loginid &"@"
& Host & ":" & data_path & filename &"
"& Windows_path)

However, during the test, when the copied file is large and the copy time is long, the CMD window will not die there, run pscp directly in the CMD window to copy the same file. I couldn't find the reason, so I tried to change it to the run method. The result of the run method can end normally, but the original code had to get the console output information of CMD, but run is not convenient to obtain console information. In the end, you can only redirect console information to the file, and then the program is reading the information. Finally, let's take a look at the differences between the run and exec methods:

The Declaration of the two methods is as follows:

Function
Exec (byval command as string) as wshexec

Function
Run (byval command as string, [byval windowstyle], [byval waitonreturn])
Integer

The differences are as follows:

1. the return value of run is an integer, that is, 0, 0, or 1, and the return value of exec is an object, you can obtain the console output information and console error information from the returned object, that is, stdout and stderr attributes. For example:

Set exers = objws. Exec ("pscp-r-SCP-PW .... "

Errmsg = exers. stderr. readall ()

Stdmsg = oexec. stdout. readall ()

You can obtain console errors and console information.

2. The last two parameters of run, one is the CMD window style, and the other is whether the execution is waiting for completion. The last parameter is very useful. If you want to wait until the end of the program executed in cmd, run the objws command. for the statement following exec, you only need to set this parameter to true. Otherwise, the subsequent statement will run directly without waiting for the completion of the CMD window. (This is the requirement for our project, after copying a file from the server to the local machine, you need to open the file immediately. If the file is not copied, the Operation will fail .). In addition, if you want to wait for the program in cmd to run the following statement when using the exec method, you can also use the following method:

Oexec. stderr. readall () or oexec. stdout. readall () should also be well understood. To get the output information, it must be output after the command is executed by CMD.

For more information about the parameters, see msdn.

Http://msdn.microsoft.com/zh-cn/library/ateytk4a (En-US, vs.85). aspx

Http://msdn.microsoft.com/zh-cn/library/d5fk67ky (En-US, vs.85). aspx

 

Related Article

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.