{"id":66293,"date":"2006-10-10T22:23:00","date_gmt":"2006-10-10T22:23:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/10\/how-can-i-provide-a-visual-indicator-when-copying-files\/"},"modified":"2006-10-10T22:23:00","modified_gmt":"2006-10-10T22:23:00","slug":"how-can-i-provide-a-visual-indicator-when-copying-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-provide-a-visual-indicator-when-copying-files\/","title":{"rendered":"How Can I Provide a Visual Indicator When Copying Files?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I provide a visual indicator of some kind when copying a large file or large set of files?<BR><BR>&#8212; SK<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, SK. Before we do anything else we should note that the Scripting Guys \u2013 or at least the Scripting Guy who writes this column \u2013 don\u2019t believe in moral victories. It\u2019s nice that the University of Washington took USC \u2013 the No. 3-ranked college football team in the country \u2013 right down to the wire on Saturday, coming within 15 yards \u2013 and a few seconds \u2013 of pulling off the upset. And while it\u2019s encouraging to see the Huskies go toe-to-toe with a team that\u2019s won 49 of its last 51 games, well, a close, hard-fought loss is still just that: a loss. But that\u2019s the way it goes.<\/P>\n<P>Of course, now that we think about it, if we were smart then we <I>would<\/I> believe in moral victories. After all, in that case we wouldn\u2019t have to write a script that could provide a visual indicator of some kind when copying a large file or large set of files; it would be enough to say that we <I>tried<\/I> to write a script that could provide a visual indicator of some kind when copying a large file or large set of files. Did we take the garbage out? No, we didn\u2019t. But the important thing is that we tried to take the garbage out; we just couldn\u2019t quite get around to it. Same for getting the oil changed or mowing the lawn. We didn\u2019t exactly do it, but the fact that we even thought about doing it should be considered a moral victory.<\/P>\n<P>But, like we said, Scripting Guys don\u2019t believe in moral victories. With that in mind, here\u2019s a script that displays the <B>Copying<\/B> dialog box while it copies files from one location to another:<\/P><PRE class=\"codeSample\">Const FOF_CREATEPROGRESSDLG = &amp;H0&amp;<\/p>\n<p>strTargetFolder = &#8220;D:\\Scripts&#8221; <\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.NameSpace(strTargetFolder) <\/p>\n<p>objFolder.CopyHere &#8220;C:\\Scripts\\*.*&#8221;, FOF_CREATEPROGRESSDLG\n<\/PRE>\n<P>As we noted, the Scripting Guys don\u2019t believe in moral victories; we also don\u2019t believe in working particularly hard. Although we could have used a combination of HTML and ActiveX controls to cobble together our own <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/activex\/progressbar.mspx\"><B>progress bar<\/B><\/A> we took the easy way out: we simply used the Windows Shell object to copy the files. Why is that so easy? Because one of the options offered by the Shell object is to display the <B>Copying<\/B> dialog box while copying files or folders. Consequently, with a minimal amount of coding and with no need for HTML pages or ActiveX controls, we were able to come up with a file copying script that displays a dialog box similar to this:<\/P><IMG border=\"0\" alt=\"Copying Files\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/copyfiles.jpg\" width=\"381\" height=\"169\"> \n<P><BR><\/P>\n<TABLE id=\"EZD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Sudden thought<\/B>. If you try really hard, yet fail, you get credit for a moral victory. So then what do you get when you don\u2019t really try all that hard and yet succeed anyway?<\/P>\n<P><I>Editor\u2019s Note: That\u2019s what\u2019s called a real victory. Something the Scripting Guys know very little about<\/I><I> \u2013 they\u2019ve got the \u201cdon\u2019t really try all that hard\u201d part down, it\u2019s the rest that tends to be a problem<\/I><I>.<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s talk about how the script works. We start out by defining a constant named FOF_CREATEPROGRESSDLG and set the value to &amp;H0&amp;; in a minute or two, we\u2019ll use this constant to tell the script that we want to display the <B>Copying<\/B> dialog box while the files are being copied. We then assign the value <I>D:\\Scripts<\/I> to a variable named strTargetFolder. This is the folder that we want to copy the files <I>to<\/I>; we won\u2019t specify the folder we want to copy files <I>from<\/I> until we actually do the copying.<\/P>\n<P>With our constant and variable taken care of the next step is to create an instance of the <B>Shell.Application<\/B> object. From there, we use this line of code to create an object reference to our target folder (D:\\Scripts):<\/P><PRE class=\"codeSample\">Set objFolder = objShell.NameSpace(strTargetFolder)\n<\/PRE>\n<P>Believe it or not, the only thing left to do is copy all the files and folders found in C:\\Scripts (that is, C:\\Scripts\\*.*) to the target folder. (And, of course, display the <B>Copying<\/B> dialog box as we do so.) That takes just one line of code:<\/P><PRE class=\"codeSample\">objFolder.CopyHere &#8220;C:\\Scripts\\*.*&#8221;, FOF_CREATEPROGRESSDLG\n<\/PRE>\n<P>As you can see, we simply call the <B>CopyHere<\/B> method, followed by the file or set of files we want to copy (in this case, C:\\Scripts\\*.*) and the constant FOF_CREATEPROGRESSDLG. That\u2019s it.<\/P>\n<P>Whoa, whoa, hold on; one question at a time. Let\u2019s start with you in the back. Does the <B>Cancel<\/B> button in the dialog box actually work? Yes, it does: if you click <B>Cancel<\/B> the copy operation will terminate. (However, it won\u2019t roll anything back: files that were copied to the target folder before the button was clicked will not be deleted from the target folder.) If you\u2019d prefer to not give people the option of cancelling the operation you have at least two choices:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Leave off the constant FOF_CREATEPROGRESSDLG when calling the CopyHere method. The copy operation will still take place; you just won\u2019t see the <B>Copying <\/B>dialog box <I>while<\/I> it takes place.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Take a different approach to copying files, and use a <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/activex\/progressbar.mspx\"><B>custom progress bar<\/B><\/A> to serve as your visual indicator.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Who\u2019s next? Yes, you in the blue shirt. \u201cCan you use this technique to copy files from your local machine to a remote computer?\u201d Yes, you can; you just need to use a UNC path when specifying the folder on the remote computer. For example, this script takes all the files found in C:\\Scripts on the local computer and copies them to C:\\Scripts on the remote computer atl-fs-01. Notice that, in this case, we use the administrative share <B>C$<\/B> as part of the folder path:<\/P><PRE class=\"codeSample\">Const FOF_CREATEPROGRESSDLG = &amp;H0&amp;<\/p>\n<p>strTargetFolder = &#8220;\\\\atl-fs-01\\C$\\scripts&#8221; <\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.NameSpace(strTargetFolder) <\/p>\n<p>objFolder.CopyHere &#8220;C:\\Scripts\\*.*&#8221;, FOF_CREATEPROGRESSDLG\n<\/PRE>\n<P>We seem to have time for one more. \u201cCan we also flip this around and copy files from a remote machine to the local computer?\u201d Of course you can. Again, the secret there is to use UNC paths. In this sample script we copy only the .VBS files (*.vbs) from atl-fs-01\u2019s shared folder Public to C:\\Scripts on the local computer:<\/P><PRE class=\"codeSample\">Const FOF_CREATEPROGRESSDLG = &amp;H0&amp;<\/p>\n<p>ParentFolder = &#8220;c:\\scripts&#8221; <\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.NameSpace(ParentFolder) <\/p>\n<p>objFolder.CopyHere &#8220;\\\\atl-fs-01\\public\\*.vbs&#8221;, FOF_CREATEPROGRESSDLG\n<\/PRE>\n<P>Sorry, that\u2019s all the time we have for today.<\/P>\n<P>We hope that helps, SK, and we hope the University of Washington gets over its disappointment and gets back on the winning track on Saturday. And now that we check the clock, it looks like we have time for one last question after all. How about the young man in the crimson and silver shirt? What\u2019s that? \u201cHow did Washington State University do this weekend?\u201d Washington State University? There must be some mistake; everyone knows there\u2019s only <I>one<\/I> university of the state of Washington.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I provide a visual indicator of some kind when copying a large file or large set of files?&#8212; SK Hey, SK. Before we do anything else we should note that the Scripting Guys \u2013 or at least the Scripting Guy who writes this column \u2013 don\u2019t believe in moral victories. [&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":[38,3,12,5],"class_list":["post-66293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I provide a visual indicator of some kind when copying a large file or large set of files?&#8212; SK Hey, SK. Before we do anything else we should note that the Scripting Guys \u2013 or at least the Scripting Guy who writes this column \u2013 don\u2019t believe in moral victories. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66293","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=66293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66293\/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=66293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}