How Can I Write Output to the Screen that Overwrites Whatever is Currently on the Screen?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I write output to the screen that overwrites whatever is currently on the screen?

— KM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KM. If you specifically need to write your output to the command window, well, then we don’t have an answer for you: although we played around with a few things, we never found an easy, straightforward way to overwrite information in the command window.

However, if you’re willing to output your information to an Internet Explorer window, well, then we do have an answer for you. And, coincidentally, here’s that answer right now:

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

Granted this looks a little long, but it’s actually pretty easy, as you’re about to find out. For example, at first glance this block of code might look a bit intimidating:

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

As it turns out, though, all we’re doing here is creating a blank instance of Internet Explorer; in particular, that’s what the first two lines of code do. The remaining lines of code simply configure various properties of the Internet Explorer window: we set the width to 400 pixels, we hide the tool bar, and we position the Internet Explorer window in the upper left-hand corner of the screen. If you’re happy with the default configuration of the Internet Explorer window then you could skip lines 3 through 8.

After we’re done setting up our Internet Explorer window we use this Do While loop to pause the script until Internet Explorer is fully loaded:

Do While (objExplorer.Busy)
    Wscript.Sleep 200
Loop

See? That’s no so bad. With Internet Explorer up and running we next give our Internet Explorer window a title and set the Visible property to True (1). We need to do that because, up till now, Internet Explorer has been running in a hidden window and has not been visible on screen. But this code will take care of that little problem:

objExplorer.Document.Title = “Process Information”   
objExplorer.Visible = 1

Now we’re ready to write something to our window. For this sample script that’s as easy as this:

objExplorer.Document.Body.InnerHTML = “Retrieving process information.”

As you can see, we’re simply assigning a value to the Document.Body object’s InnerHTML property. In this case we’re merely assigning InnerHTML some text: Retrieving process information. However, we could easily add some HTML tags and create fancier output. For example, this line of code will boldface the text written to the window:

objExplorer.Document.Body.InnerHTML = “<B>Retrieving process information.</B>”

OK, maybe that doesn’t qualify as fancy. But you get the idea.

After displaying our text string in the Internet Explorer window we then pause the script for 2 seconds (2000 milliseconds). There’s no reason why we have to do that; that’s done simply so you’ll have a chance to see the text before we overwrite it.

That brings us to this block of 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’re doing here is retrieving a collection of the processes running on the local computer. That’s not important; we just need the script to do something interesting. What we care about today is what happens inside the For Each loop we set up to walk through all the items in that collection:

For Each objItem in colItems
    objExplorer.Document.Body.InnerHTML = objItem.Name
    Wscript.Sleep 500
Next

In most WMI scripts we’d echo back property values (such as Name) inside our For Each loop. Here, however, we don’t use Wscript.Echo. Instead, we assign the name of the first process in the collection to the body’s InnerHTML property:

objExplorer.Document.Body.InnerHTML = objItem.Name

What’s that going to do? That’s going to overwrite the existing contents of our Internet Explorer window with the name of the first process in the collection. For example, at the time we begin the loop the Internet Explorer window will contain this text:

Retrieving process information.

As we start the loop, that text will be replaced with the name of the first process in the collection. For example:

Winword.exe.

After overwriting the contents of the window we pause the script for half a second (500 milliseconds). Again, that’s not required; we do that simply to slow the script down a little and give you a chance to see the first process name displayed. Half a second later we loop around and retrieve information about the second process in the collection. The script then replaces the existing contents of the Internet Explorer window (technically, the value assigned to the InnerHTML property) with the name of the second process. Etc. etc.

After we’ve looped through the entire collection we indicate that the script has finished, pause for 3 seconds, and then dismiss the Internet Explorer window:

objExplorer.Document.Body.InnerHTML = “Process information retrieved.”
Wscript.Sleep 3000
objExplorer.Quit

This works pretty good, and is pretty easy as well. And because it uses HTML that means you can make your output as fancy as you want; in fact, you could even make your Internet Explorer window look like a command window. If that’s not cool, well, we don’t know what is. (Actually, as one of the Scripting Sons constantly reminds his Scripting Father, we don’t know what cool is.)

0 comments

Discussion is closed.

Feedback usabilla icon