{"id":70223,"date":"2005-03-16T19:37:00","date_gmt":"2005-03-16T19:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/16\/how-can-i-display-a-progress-bar-or-something-similar-while-my-script-runs\/"},"modified":"2005-03-16T19:37:00","modified_gmt":"2005-03-16T19:37:00","slug":"how-can-i-display-a-progress-bar-or-something-similar-while-my-script-runs","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-display-a-progress-bar-or-something-similar-while-my-script-runs\/","title":{"rendered":"How Can I Display A Progress Bar (or Something Similar) While My Script Runs?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I get my script to do something interesting while the code executes; you know, how can I display a progress bar or something?<BR><BR>&#8212; HD<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, HD. To begin with, we don\u2019t recommend that you try to use a true progress bar; that\u2019s because it\u2019s very difficult to calculate &#8211; let alone display &#8211; progress. We\u2019re all familiar with those so-called progress bars &#8211; some, alas, included in Microsoft products &#8211; that tell you that the estimated time to complete an operation is 3 minutes, then 296 minutes, then 1 minute, then 14 minutes. We don\u2019t want to mess around with something like that.<\/P>\n<P>Instead, we suggest you try a simple little dialog box (or at least something that <I>looks<\/I> like a dialog box) that merely informs the user that something is going on and asks them to be patient. When the operation is complete, our sample dialog box displays a message to that effect, and then disappears. It\u2019s not fancy, but it works.<\/P>\n<P>Here\u2019s the code:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objExplorer = CreateObject _\n    (&#8220;InternetExplorer.Application&#8221;)<\/p>\n<p>objExplorer.Navigate &#8220;about:blank&#8221;   \nobjExplorer.ToolBar = 0\nobjExplorer.StatusBar = 0\nobjExplorer.Width = 400\nobjExplorer.Height = 200 \nobjExplorer.Visible = 1             <\/p>\n<p>objExplorer.Document.Title = &#8220;Logon script in progress&#8221;\nobjExplorer.Document.Body.InnerHTML = &#8220;Your logon script is being processed. &#8221; _\n    &amp; &#8220;This might take several minutes to complete.&#8221;<\/p>\n<p>Wscript.Sleep 10000<\/p>\n<p>objExplorer.Document.Body.InnerHTML = &#8220;Your logon script is now complete.&#8221;<\/p>\n<p>Wscript.Sleep 5000\nobjExplorer.Quit\n<\/PRE>\n<P>All we\u2019re doing here is creating an instance of Internet Explorer, then using this line of code to open up a blank page in the browser window:<\/P><PRE class=\"codeSample\">objExplorer.Navigate &#8220;about:blank&#8221;\n<\/PRE>\n<P>We get rid of the toolbar and the status bar (by setting those values to 0), and then set the size of our window to 400 pixels and 200 pixels. We then set the Visible property to 1, which actually displays our little Internet Explorer window on the screen. And just for the heck of it we configure the Title property for the window using this line of code:<\/P><PRE class=\"codeSample\">objExplorer.Document.Title = &#8220;Logon script in progress&#8221;\n<\/PRE>\n<P>The net result? Something that looks like this:<\/P><IMG height=\"220\" alt=\"Internet Explorer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/ie3.jpg\" width=\"350\" border=\"0\"> \n<P>That\u2019s almost OK in and of itself, but we can probably do a little better: for one thing, we can display a custom message in our Internet Explorer document. To do that we set the InnerHTML property of the document body:<\/P><PRE class=\"codeSample\">objExplorer.Document.Body.InnerHTML = &#8220;Your logon script is being processed. &#8221; _\n    &amp; &#8220;This might take several minutes to complete.&#8221;\n<\/PRE>\n<P>What\u2019s cool about this is that we can use all our favorite HTML tags when assigning a value to the InnerHTML property. For example, suppose we want this message displayed in bold. In that case, we just need to include the &lt;B&gt; and &lt;\/B&gt; tags:<\/P><PRE class=\"codeSample\">objExplorer.Document.Body.InnerHTML = &#8220;&lt;B&gt;Your logon script is being processed. &#8221; _\n    &amp; &#8220;This might take several minutes to complete.&lt;\/B&gt;&#8221;\n<\/PRE>\n<P>After setting the InnerHTML property, we have an instance of Internet Explorer that looks like this:<\/P><IMG height=\"220\" alt=\"Internet Explorer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/ie2.jpg\" width=\"350\" border=\"0\"> \n<P>Not too bad, huh? In our sample script, we pause for 10 seconds and then replace our old message with a new message, one that informs the user that their logon script has completed. We pause for 5 more seconds, and then dismiss our instance of Internet Explorer.<\/P>\n<P>If you want to get a little fancier, you can do a couple things. In the revised script we\u2019ll show you momentarily, we use the WMI class Win32_DesktopMonitor to determine the current video resolution (e.g., 1024&#215;768). We then use some simple math to position the IE window in the middle of the screen. For example, if our screen is 1024 pixels wide, we subtract 400 (the width of our Internet Explorer window) from 1024. We divide that number by two, leaving us with the pixel position for the left side of our window. Repeating this trick with the display height (768) gives us the pixel position for the top of our window, and thus centers our dialog box on screen. Here\u2019s the code that gets the screen width and height:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;Winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_DesktopMonitor&#8221;)\nFor Each objItem in colItems\n    intHorizontal = objItem.ScreenWidth\n    intVertical = objItem.ScreenHeight\nNext\n<\/PRE>\n<P>And here are the two lines of code that position the window on screen:<\/P><PRE class=\"codeSample\">objExplorer.Left = (intHorizontal &#8211; 400) \/ 2\nobjExplorer.Top = (intVertical &#8211; 200) \/ 2\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E1D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. The preceding code is really designed for computers that have only a single monitor; things get a bit more complicated on a multi-monitor system, especially if one of those monitors is turned off. For now, we\u2019ll assume a single monitor; we\u2019ll deal with the multi-monitor issue somewhere down the road.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In addition to centering the Internet Explorer window, we reinforce the fact that the user needs to wait for a moment by setting the cursor to an hourglass. That\u2019s done with this line of code:<\/P><PRE class=\"codeSample\">objExplorer.Document.Body.Style.Cursor = &#8220;wait&#8221;\n<\/PRE>\n<P>Later in the script we set the cursor to <B>default<\/B>, which dismisses the hourglass and brings back the standard arrow cursor.<\/P>\n<P>Here\u2019s what our new and improved script looks like:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;Winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_DesktopMonitor&#8221;)\nFor Each objItem in colItems\n    intHorizontal = objItem.ScreenWidth\n    intVertical = objItem.ScreenHeight\nNext<\/p>\n<p>Set objExplorer = CreateObject _\n    (&#8220;InternetExplorer.Application&#8221;)<\/p>\n<p>objExplorer.Navigate &#8220;about:blank&#8221;   \nobjExplorer.ToolBar = 0\nobjExplorer.StatusBar = 0\nobjExplorer.Left = (intHorizontal &#8211; 400) \/ 2\nobjExplorer.Top = (intVertical &#8211; 200) \/ 2\nobjExplorer.Width = 400\nobjExplorer.Height = 200 \nobjExplorer.Visible = 1             <\/p>\n<p>objExplorer.Document.Body.Style.Cursor = &#8220;wait&#8221;<\/p>\n<p>objExplorer.Document.Title = &#8220;Logon script in progress&#8221;\nobjExplorer.Document.Body.InnerHTML = &#8220;Your logon script is being processed. &#8221; _\n    &amp; &#8220;This might take several minutes to complete.&#8221;<\/p>\n<p>Wscript.Sleep 10000<\/p>\n<p>objExplorer.Document.Body.InnerHTML = &#8220;Your logon script is now complete.&#8221;<\/p>\n<p>objExplorer.Document.Body.Style.Cursor = &#8220;default&#8221;<\/p>\n<p>Wscript.Sleep 5000<\/p>\n<p>objExplorer.Quit\n<\/PRE>\n<P><I>Still<\/I> not fancy enough for you? Well, another way to spice things up is to include an animated .GIF in your InnerHTML. For example, this line of code displays an animated .GIF as well as a message:<\/P><PRE class=\"codeSample\">objExplorer.Document.Title = &#8220;Logon script in progress&#8221;\nobjExplorer.Document.Body.InnerHTML = &#8220;&lt;img src=&#8217;file:\/\/\/C:\\Scripts\\watch.gif&#8217;&gt; &#8221; &amp; _\n    &#8220;Your logon script is being processed. This might take several minutes to complete.&#8221;\n<\/PRE>\n<P>The end result:<\/P><IMG height=\"220\" alt=\"Internet Explorer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/ie1.jpg\" width=\"350\" border=\"0\"> \n<P>It might not qualify as art, but we think it <I>does<\/I> qualify as doing \u201c\u2026something interesting while the code executes.\u201d And, remember, you can change the picture alignment, you can change the font size and color &#8212; you can pretty much do anything HTML allows you to do.<\/P>\n<P><B>Note<\/B>. If you want to do something <I>really<\/I> interesting while your code executes, visit <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/default.mspx\"><B>Dr. Scripto\u2019s Fun Zone<\/B><\/A> and learn how you can incorporate Microsoft Agent technology into your scripts.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get my script to do something interesting while the code executes; you know, how can I display a progress bar or something?&#8212; HD Hey, HD. To begin with, we don\u2019t recommend that you try to use a true progress bar; that\u2019s because it\u2019s very difficult to calculate &#8211; let [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-70223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get my script to do something interesting while the code executes; you know, how can I display a progress bar or something?&#8212; HD Hey, HD. To begin with, we don\u2019t recommend that you try to use a true progress bar; that\u2019s because it\u2019s very difficult to calculate &#8211; let [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}