Using vbs to write the output content to the screen to overwrite the content on the current screen

Source: Internet
Author: User

Q:

Hello, script expert! How can I write the output content to the screen to overwrite the content on the current screen?

-- KM

A:

Hello, KM. If you do need to write the output content to the Command window, we cannot provide you with the answer: Although we have already handled some problems, however, we have never found a simple and direct method to overwrite the information in the Command window.

However, if you want to output the information to the Internet Explorer window, we do have a solution. And you can give the answer immediately:

Set objExplorer = CreateObject ("InternetExplorer. Application ")

ObjExplorer. Navigate "about: blank"

ObjExplorer. ToolBar = 0

ObjExplorer. StatusBar = 0

Object explorer. Width = 400

Object explorer. Height = 200

ObjExplorer. Left = 0

ObjExplorer. Top = 0

Do While (objExplorer. Busy)

Wscript. Sleep 200

Loop

ObjExplorer. Document. Title = "Process Information"

ObjExplorer. Visible = 1

ObjExplorer. Document. Body. InnerHTML = "Retrieving process information ."

Wscript. Sleep 2000

StrComputer = "."

Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")

Set colItems = ob1_miservice. ExecQuery ("Select * from Win32_Process ")

For Each objItem in colItems

ObjExplorer. Document. Body. InnerHTML = objItem. Name

Wscript. Sleep 500

Next

ObjExplorer. Document. Body. InnerHTML = "Process information retrieved ."

Wscript. Sleep 3000

ObjExplorer. Quit

It looks a little long, but it is actually very simple, and you will soon find it. For example, at first glance, this Code may be quite scary:

Set objExplorer = CreateObject ("InternetExplorer. Application ")

ObjExplorer. Navigate "about: blank"

ObjExplorer. ToolBar = 0

ObjExplorer. StatusBar = 0

Object explorer. Width = 400

Object explorer. Height = 200

ObjExplorer. Left = 0

ObjExplorer. Top = 0

However, it turns out that all we do is create an empty Internet Explorer instance, and this is done through the first two lines of code. Other code lines only configure various properties of the Internet Explorer window: we set the width to 400 pixels, hide the toolbar, and then locate the Internet Explorer window in the upper left corner of the screen. If you are willing to use the default configuration of the Internet Explorer window, you can skip 3 to 8 lines.

After setting the Internet Explorer window, we use the Do While loop below to stop the script after fully loading Internet Explorer:

Do While (objExplorer. Busy)

Wscript. Sleep 200

Loop

Have you seen it? Not bad. After you start and run Internet Explorer, specify a title for the Internet Explorer window and set the Visible attribute to True (1 ). So far, Internet Explorer is still running in a hidden window and cannot be seen on the screen. However, the following code solves this problem:

ObjExplorer. Document. Title = "Process Information"

ObjExplorer. Visible = 1

Now we are going to write something in the window. This sample script is as simple as the following code:

ObjExplorer. Document. Body. InnerHTML = "Retrieving process information ."

As you can see, we only assign a value to the InnerHTML attribute of the Document. Body object. In this case, we only assign some text to InnerHTML: To retrieve process information. However, we can easily add some HTML tags and create more unique outputs. For example, this line of code will bold the text written to the window:

ObjExplorer. Document. Body. InnerHTML = "<B> Retrieving process information. </B>"

Well, maybe this is not exactly consistent with the idea. However, you have obtained the answer to the question.

After the text string is displayed in the Internet Explorer window, we can abort the script for 2 seconds (2000 milliseconds ). There is no reason to do this, but you can see them before overwriting the original text.

Next is the following code:

StrComputer = "."

Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")

Set colItems = ob1_miservice. ExecQuery ("Select * from Win32_Process ")

For Each objItem in colItems

ObjExplorer. Document. Body. InnerHTML = objItem. Name

Wscript. Sleep 500

Next

We only need to retrieve the set of processes running on the local computer. This is not very important; we just need to get some interesting results by running the script. What we care about today is what will happen in the For Each loop we create to traverse all items in the set:

For Each objItem in colItems

ObjExplorer. Document. Body. InnerHTML = objItem. Name

Wscript. Sleep 500

Next

In most WMI scripts, we display the attribute values (For example, Name) in the For Each loop ). However, we do not use Wscript. Echo, but assign the name of the first process in the set to the InnerHTML attribute of the script body:

ObjExplorer. Document. Body. InnerHTML = objItem. Name

What are the results? Overwrite the existing content in the Internet Explorer window with the name of the first process in the set. For example, to start a loop, the Internet Explorer window contains the following text:

Search process information.

When we start the loop, this text will be replaced with the name of the first process in the set. For example:

Winword.exe.

After overwriting the content in the window, we abort the script for half a second (500 ms ). Once again, this is not a necessary task; we only do this to speed down the script so that you can see the name of the first process displayed. After half a second, enter the next loop and retrieve information about the second process in the collection. Then, the script replaces the existing content in the Internet Explorer window with the name of the second process (technically, it is the value assigned to the InnerHTML attribute), and so on.

After traversing the entire set, we indicate that the script has ended, abort for 3 seconds, and then close the Internet Explorer window:

ObjExplorer. Document. Body. InnerHTML = "Process information retrieved ."

Wscript. Sleep 3000

ObjExplorer. Quit

The running results are very good and simple. Because it uses HTML, it means that you can make the output as you wish; in fact, you can even make the Internet Explorer window look like a command window. If this is not cool enough, we don't know how to make it cool. (As a matter of fact, as a script expert son often reminds his script expert father, we don't know what cool is .)

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.