Method of writing output to the screen with VBS to overwrite content on the current screen _vbs

Source: Internet
Author: User
Tags html tags
Ask:
Hello, Scripting Guy! How do I write the output to the screen to overwrite what is on the current screen?
--KM
For:
Hello, KM. If you really need to write the output to the command window, then we have no way to provide you with the answer: although we have dealt with some of the problems, we have never found a simple, straightforward way to overwrite the information in the command window.
However, if you are exporting information to an Internet Explorer window, then we do have a solution. And you can give the answer right away:
Set Objexplorer = CreateObject ("Internetexplorer.application")
Objexplorer.navigate "About:blank"
Objexplorer.toolbar = 0
Objexplorer.statusbar = 0
Objexplorer.width = 400
Objexplorer.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 objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set colitems = objWMIService.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 does look a little long, but it's actually very simple, and you'll find out soon enough. At first glance, for example, this code might be scary:
Set Objexplorer = CreateObject ("Internetexplorer.application")
Objexplorer.navigate "About:blank"
Objexplorer.toolbar = 0
Objexplorer.statusbar = 0
Objexplorer.width = 400
Objexplorer.height = 200
Objexplorer.left = 0
Objexplorer.top = 0
However, it turns out that all we do is create an empty instance of Internet Explorer, and this is done with the first two lines of code. The remaining lines of code are only configured with the various properties of the Internet Explorer window: We set the width to 400 pixels, hide the toolbar, and then position the Internet Explorer window in the upper-left corner of the screen. If you prefer to use the default configuration of the Internet Explorer window, you can skip 3 to 8 rows.
After setting up the Internet Explorer window, we use the following do-while loop to abort the script after fully loading Internet Explorer:
Do while (OBJEXPLORER.BUSY)
Wscript.Sleep 200
Loop
Did you see it? It's not bad. After you start and run Internet Explorer, you then specify a caption for the Internet Explorer window and set the Visible property to True (1). The reason we do this is that, so far, Internet Explorer is still running in hidden windows and not visible on the screen. But the following code resolves this little problem:
ObjExplorer.Document.Title = "Process Information"
objexplorer.visible = 1
Now we're going to write something in the window. For this sample script, it's as simple as the following code:
ObjExplorer.Document.Body.InnerHTML = "Retrieving process information."
As you can see, we're just assigning a value to the InnerHTML property of the Document.body object. In this case, we only give InnerHTML some text: Retrieve process information. However, we can easily add some HTML tags and create more unique output. For example, this line of code will make text that is written to a window bold:
ObjExplorer.Document.Body.InnerHTML = "<b>retrieving process information.</b>"
Well, maybe it's not exactly the idea. But you've got the answer to the question.
After displaying our text string in the Internet Explorer window, we can abort the script for 2 seconds (2000 milliseconds). There is no reason to do this, just so you can see them before overwriting the original text.
Next is the following code:
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set colitems = objWMIService.ExecQuery ("SELECT * from Win32_Process")
For each objitem in colitems
ObjExplorer.Document.Body.InnerHTML = objItem.Name
Wscript.Sleep 500
Next
All we need to do is retrieve the collection 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 happens in the For Each loop that we established to traverse all the items in the collection:
For each objitem in colitems
ObjExplorer.Document.Body.InnerHTML = objItem.Name
Wscript.Sleep 500
Next
In most WMI scripts, we echo the property values (for example, Name) within the For Each loop. However, instead of using WScript.Echo, we assign the name of the first process in the collection to the InnerHTML property of the script body:
ObjExplorer.Document.Body.InnerHTML = objItem.Name
What will be the result of this? The existing contents of the Internet Explorer window will be overwritten with the name of the first process in the collection. For example, when we start looping, the Internet Explorer window will contain the following text:
Retrieves process information.
When we start the loop, the text is replaced with the name of the first process in the collection. For example:
Winword.exe.
After overwriting the contents of the window, we aborted the script for half a second (500 milliseconds). Again, this is not something that has to be done; we do this only to slow down the script so that you can see the name of the first process displayed. After half a second, go to the next loop and retrieve information about the second process in the collection. The script then replaces the existing content in the Internet Explorer window (technically, the value assigned to the InnerHTML property) with the name of the second process, and so on.
After traversing the entire collection, we instruct the script to end, abort for 3 seconds, and then close the Internet Explorer window:
ObjExplorer.Document.Body.InnerHTML = "Process information retrieved."
Wscript.Sleep 3000
Objexplorer.quit
It works very well and is very simple. Because it uses HTML, which means you can make the output as you wish; in fact, you can even make an Internet Explorer window look like a command window. If it's not cool enough, then we don't know what's cool. (In fact, as a Scripting Guy's son often reminds his scripting 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.