{"id":69573,"date":"2005-06-17T21:15:00","date_gmt":"2005-06-17T21:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/17\/how-can-i-show-users-a-dialog-box-that-only-lets-them-select-folders\/"},"modified":"2005-06-17T21:15:00","modified_gmt":"2005-06-17T21:15:00","slug":"how-can-i-show-users-a-dialog-box-that-only-lets-them-select-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-show-users-a-dialog-box-that-only-lets-them-select-folders\/","title":{"rendered":"How Can I Show Users a Dialog Box That Only Lets Them Select Folders?"},"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! In previous columns you\u2019ve shown us how to present users with a dialog box that enables them to select files. Is there any way to show them a dialog box that only lets users select folders?<BR><BR>&#8212; LP<\/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, LP. You know, you\u2019ve come to the right place: the Scripting Guys have just what the doctor ordered.<\/P>\n<P>Actually, we have the <B>BrowseForFolder<\/B> method, which is part of the Windows <B>Shell<\/B> object. If that\u2019s the sort of thing your doctor is prescribing you might think very seriously about getting a second opinion.<\/P>\n<P>But while we\u2019d be a bit suspicious of a doctor who\u2019s prescribing the BrowseForFolder method we have no qualms about prescribing this ourselves. Let\u2019s show you a sample script, and then we\u2019ll talk about how it works:<\/P><PRE class=\"codeSample\">Const WINDOW_HANDLE = 0\nConst OPTIONS = 0<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.BrowseForFolder _\n    (WINDOW_HANDLE, &#8220;Select a folder:&#8221;, OPTIONS, &#8220;C:\\&#8221;) <\/p>\n<p>If objFolder Is Nothing Then\n    Wscript.Quit\nEnd If<\/p>\n<p>Set objFolderItem = objFolder.Self\nobjPath = objFolderItem.Path<\/p>\n<p>Wscript.Echo objPath\n<\/PRE>\n<P>We begin by defining a pair of constants: WINDOW_HANDLE and OPTIONS. The WINDOW_HANDLE constant represents a numeric ID that needs to be assigned to the dialog box we\u2019re going to display; for scripts this value should <I>always<\/I> be 0. Setting OPTIONS to 0 means we\u2019re going to display a very simple dialog box, one that limits users to selecting from a list of folders. Alternatively, we could have set OPTIONS to &amp;H10&amp;. In that case our dialog box would include a text area where users could type a folder path.<\/P>\n<P>After defining our constants we create an instance of the <B>Shell.Application<\/B> object and then use this code to display a browse-for-folder dialog box:<\/P><PRE class=\"codeSample\">Set objFolder = objShell.BrowseForFolder _\n    (WINDOW_HANDLE, &#8220;Select a folder:&#8221;, OPTIONS, &#8220;C:\\&#8221;)\n<\/PRE>\n<P>As you can see, we merely call the BrowseForFolder method, passing four parameters:<\/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>WINDOW_HANDLE, which, as we noted, is the numeric ID assigned to the dialog box window.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The text string <B>Select a folder:<\/B>, which will serve as the instructional message displayed in the dialog box.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>OPTIONS, the constant representing the options used in constructing the dialog box.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>C:\\<\/B>, which serves as the root folder for our dialog box. The dialog box will open in C:\\ and will not allow you to select any file locations higher up in the tree (for example, you can\u2019t select My Computer). Had we set the root folder to C:\\Scripts then the user would be allowed to select only the folder C:\\Scripts and any of its subfolders.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That code will result in a dialog box similar to this appearing on screen:<\/P><IMG border=\"0\" alt=\"Browse For Folder Dialog Box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/browse1.jpg\" width=\"326\" height=\"322\"> \n<P><BR>(If you\u2019re wondering, yes, you\u2019ve seen this dialog box before. Many Windows applications make use of this same method and same dialog box.)<\/P>\n<P>At this point our script pauses, waiting for the user to either select a folder and click <B>OK<\/B> or to click <B>Cancel<\/B>. When the user does one of these two things the dialog box is dismissed and the actions are stored in the object reference objFolder.<\/P>\n<P>So how do we know whether the user selected a folder and clicked <B>OK<\/B> or whether the user simply clicked <B>Cancel<\/B>? That\u2019s what this block of code is for:<\/P><PRE class=\"codeSample\">If objFolder Is Nothing Then\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>This code checks our object reference (objFolder) to see if it happens to be equal to a real object (that\u2019s what the <B>Nothing<\/B> keyword is for). If objFolder <I>is<\/I> equal to Nothing that means the user clicked <B>Cancel<\/B>; in that case we simply use <B>Wscript.Quit<\/B> to exit the script. If objFolder is not equal to Nothing then objFolder must refer to a real object; therefore the script continues on its merry way.<\/P>\n<P>The next two lines of code are required due to a quirk in the Shell object:<\/P><PRE class=\"codeSample\">Set objFolderItem = objFolder.Self\nobjPath = objFolderItem.Path\n<\/PRE>\n<P>When a user selects a folder and clicks <B>OK<\/B> they get back an instance of the Shell <B>Folder<\/B> object. For some reason, however, you can\u2019t do anything with a Shell Folder object; if we want to retrieve the path to the selected folder we have to use a <B>FolderItem<\/B> object instead. (Why? We have no idea.) Therefore our first line of code uses the <B>Self<\/B> method to return a FolderItem object that happens to be identical to our Folder object. The second line of code then stores the <B>Path<\/B> to that FolderItem object in a variable named objPath. A bit awkward, but it works.<\/P>\n<P>Finally, we echo back the path to the selected folder and we\u2019re done:<\/P><IMG border=\"0\" alt=\"Browse For Folder Dialog Box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/browse2.jpg\" width=\"170\" height=\"107\"> \n<P><BR>As we noted, our sample dialog box uses C:\\ as the root folder and won\u2019t let you select folders located anywhere else on the computer. Sometimes that\u2019s good; that forces users to choose from a certain set of folders. At other times, however, you\u2019d like to give users the opportunity to select any folder located anywhere on the file system. Is that possible?<\/P>\n<P>You bet it is. We won\u2019t go into the details of this modified script, but this one sets My Computer as the root folder:<\/P><PRE class=\"codeSample\">Const MY_COMPUTER = &amp;H11&amp;\nConst WINDOW_HANDLE = 0\nConst OPTIONS = 0<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(MY_COMPUTER)\nSet objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.BrowseForFolder _\n    (WINDOW_HANDLE, &#8220;Select a folder:&#8221;, OPTIONS, strPath) <\/p>\n<p>If objFolder Is Nothing Then\n    Wscript.Quit\nEnd If<\/p>\n<p>Set objFolderItem = objFolder.Self\nobjPath = objFolderItem.Path<\/p>\n<p>Wscript.Echo objPath\n<\/PRE>\n<P>As you might expect, the resulting dialog box gives you plenty of options to choose from:<\/P><IMG border=\"0\" alt=\"Browse For Folder Dialog Box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/browse3.jpg\" width=\"326\" height=\"322\"> \n<P><BR>Just what you needed. And, hopefully, <I>not<\/I> what the doctor ordered.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In previous columns you\u2019ve shown us how to present users with a dialog box that enables them to select files. Is there any way to show them a dialog box that only lets users select folders?&#8212; LP Hey, LP. You know, you\u2019ve come to the right place: the Scripting Guys have just [&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-69573","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! In previous columns you\u2019ve shown us how to present users with a dialog box that enables them to select files. Is there any way to show them a dialog box that only lets users select folders?&#8212; LP Hey, LP. You know, you\u2019ve come to the right place: the Scripting Guys have just [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69573","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=69573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69573\/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=69573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}