{"id":70553,"date":"2005-01-28T16:52:00","date_gmt":"2005-01-28T16:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/28\/how-can-i-show-users-a-dialog-box-for-selecting-files\/"},"modified":"2005-01-28T16:52:00","modified_gmt":"2005-01-28T16:52:00","slug":"how-can-i-show-users-a-dialog-box-for-selecting-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-show-users-a-dialog-box-for-selecting-files\/","title":{"rendered":"How Can I Show Users a Dialog Box for Selecting Files?"},"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! Is there any way I can use a script to present a user with a dialog box and let him or her select a file?<BR><BR>&#8212; BF<\/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, BF. If you\u2019re using Windows 2000, we don\u2019t know of a way to do this, at least not a way that\u2019s built into the operating system. That\u2019s not the case with Windows XP, however. On Windows XP, you can use the <B>UserAccounts.CommonDialog<\/B> object to present users with a standard File Open dialog box. Here\u2019s what the script looks like:<\/P><PRE class=\"codeSample\">Set objDialog = CreateObject(&#8220;UserAccounts.CommonDialog&#8221;)<\/p>\n<p>objDialog.Filter = &#8220;All Files|*.*&#8221;\nobjDialog.InitialDir = &#8220;C:\\&#8221;\nintResult = objDialog.ShowOpen<\/p>\n<p>If intResult = 0 Then\n    Wscript.Quit\nElse\n    Wscript.Echo objDialog.FileName\nEnd If\n<\/PRE>\n<P>It\u2019s a small script, so let\u2019s walk through it line-by-line. We begin by creating an object reference (named <B>objDialog<\/B>) to the UserAccounts.CommonDialog object. Next we set the <B>Filter<\/B> property for the dialog box. We want to show all files, so we set the filter like so:<\/P><PRE class=\"codeSample\">objDialog.Filter = &#8220;All Files|*.*&#8221;\n<\/PRE>\n<P>Suppose we wanted to show only text files? Well, in that case we\u2019d use this filter:<\/P><PRE class=\"codeSample\">objDialog.Filter = &#8220;Text Files|*.txt&#8221;\n<\/PRE>\n<P>You can probably see how this works: we provide a description of the file types (<B>Text Files<\/B>), toss in a \u201cpipe separator\u201d (<B>|<\/B>) and then use a standard wildcard to indicate all .txt files (<B>*.txt<\/B>). Want to get fancy and show.txt files by default, but then give people the option of looking at all files? Then use this code:<\/P><PRE class=\"codeSample\">objDialog.Filter = &#8220;Text Files|*.txt|All Files|*.*&#8221;\n<\/PRE>\n<P>Try it, and you\u2019ll see what we mean.<\/P>\n<P>We then specify the default folder. By default, we want the dialog box to show the file in the root folder of drive C, so we set the <B>InitialDir<\/B> property like this:<\/P><PRE class=\"codeSample\">objDialog.InitialDir = &#8220;C:\\&#8221;\n<\/PRE>\n<P>Want to show files in the C:\\Windows folder instead? Then use this code:<\/P><PRE class=\"codeSample\">objDialog.InitialDir = &#8220;C:\\Windows&#8221;\n<\/PRE>\n<P>And don\u2019t worry: this is a real File Open dialog box, so you can click around and end up anywhere you want. Just because you <I>start<\/I> in C:\\Windows doesn\u2019t mean that\u2019s the only folder where you can open files.<\/P>\n<P>Finally, we display the dialog box using this line of code:<\/P><PRE class=\"codeSample\">intResult = objDialog.ShowOpen\n<\/PRE>\n<P>Now we just sit and wait for the user to select a file and click <B>OK<\/B> (or &#8211; alternatively &#8211; for the user to click Cancel). If the user clicks <B>Cancel<\/B>, the variable intResult will be set to 0. In our script, we check the value of intResult and, if it\u2019s 0, we simply use Wscript.Quit to terminate the script.<\/P>\n<P>But what if the user actually selects a file and then clicks <B>OK<\/B>? In that case, intResult will be set to -1, and the <B>FileDialog<\/B> property will be set to the path name of the selected file. Our script simply echoes back the path name, meaning we\u2019ll get output similar to this:<\/P><PRE class=\"codeSample\">C:\\WINDOWS\\Prairie Wind.bmp\n<\/PRE>\n<P>Needless to say, however, you\u2019re not restricted to merely echoing back the file path. Instead, you could use WMI or the FileSystemObject or some other technology to bind to that file and delete it, copy it, compress it, retrieve the file properties, or do pretty much anything you can do to a file. <\/P>\n<P>Well, with a script anyway.<\/P>\n<P>Incidentally, you can only select a single file at a time with this approach; no holding down the <B>Ctrl<\/B> key and selecting multiple files. There <I>is<\/I> a way to select multiple files, at least on an XP machine, but we\u2019ll have to address that in a future column.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there any way I can use a script to present a user with a dialog box and let him or her select a file?&#8212; BF Hey, BF. If you\u2019re using Windows 2000, we don\u2019t know of a way to do this, at least not a way that\u2019s built into the operating [&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":[15,3,4,5],"class_list":["post-70553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dialog-boxes","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there any way I can use a script to present a user with a dialog box and let him or her select a file?&#8212; BF Hey, BF. If you\u2019re using Windows 2000, we don\u2019t know of a way to do this, at least not a way that\u2019s built into the operating [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70553","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=70553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70553\/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=70553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}