{"id":70333,"date":"2005-03-01T14:54:00","date_gmt":"2005-03-01T14:54:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/01\/how-can-i-use-a-multi-select-dialog-box-in-a-script\/"},"modified":"2005-03-01T14:54:00","modified_gmt":"2005-03-01T14:54:00","slug":"how-can-i-use-a-multi-select-dialog-box-in-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-a-multi-select-dialog-box-in-a-script\/","title":{"rendered":"How Can I Use a Multi-Select Dialog Box in a Script?"},"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! Awhile back you showed us how to use a File Open dialog box in our scripts. At the same time you said you\u2019d show us how to use a multi-select dialog box. Did you just forget about the multi-select dialog box?<BR><BR>&#8212; AA<\/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, AA. Yes, we \u2026 forgot \u2026 about the multi-select dialog box. (Saying we forgot sounds better than saying, \u201cNo, we just irresponsibly said we\u2019d do something and then failed to follow up on it.\u201d) For those of you who missed our original column on adding a File Open dialog box to a script, you\u2019ll find it <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jan05\/hey0128.mspx\"><B>here<\/B><\/A>. For the rest of you, here\u2019s a script that uses a multi-select File Open dialog box. (Note: By \u201cmulti-select\u201d we simply mean a dialog box that allows you to select multiple files. The dialog box we showed in a previous column limited you to selecting a single file.)<\/P><PRE class=\"codeSample\">Set objDialog = CreateObject(&#8220;UserAccounts.CommonDialog&#8221;)\nobjDialog.Filter = &#8220;VBScript Scripts|*.vbs|All Files|*.*&#8221;\nobjDialog.Flags = &amp;H0200\nobjDialog.FilterIndex = 1\nobjDialog.InitialDir = &#8220;C:\\Scripts&#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>There are a few things we need to explain here, beginning with the fact that this dialog box is available only on Windows XP; the preceding script won\u2019t work on any other version of Windows, including Windows Server 2003. Second, there\u2019s only one difference between this and the script we showed you a couple months ago. If you want to create a multi-select dialog box, you simply need to add this line of code:<\/P><PRE class=\"codeSample\">objDialog.Flags = &amp;H0200\n<\/PRE>\n<P>That\u2019s it: set the <B>Flags<\/B> property to &amp;H0200, and you\u2019ll have a multi-select File Open dialog box. To select multiple files in the dialog box, just do what you usually do: click the first file, then hold down the <B>Ctrl<\/B> key and click any additional files you want to select.<\/P>\n<P>Having said that, we need to point out that the File Open dialog box you get, while fully functional, isn\u2019t as nice as the File Open dialog box you\u2019re used to. For one thing, you don\u2019t get the nice shortcut bar (with the icons for things like <B>Desktop<\/B> and <B>My Documents<\/B>). For another, the dialog box displays everything using short file names; in other words, the dialog box is going to look something like this:<\/P><IMG height=\"250\" alt=\"Multi-Select Dialog Box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/multiselect.jpg\" width=\"408\" border=\"0\"> \n<P><BR>Fully functional, but not as aesthetically pleasing as the other File Open dialog box, the one that allows you to select only a single file.<\/P>\n<P>In addition, the return value from the multi-select dialog box is a string that consists of the folder name and the short file names of all the files you selected. In this string, individual items are separated by a blank space. Thus you get back something like this:<\/P><PRE class=\"codeSample\">C:\\Scripts\\ file_dialog.vbs file_save.vbs test.vbs\n<\/PRE>\n<P>That means we need to do some additional coding here: we need to take the folder path (<B>C:\\Scripts\\<\/B>) and add on the individual file names in order to create valid file paths. Let\u2019s take a look at a revised script that echoes back the file paths for each file selected:<\/P><PRE class=\"codeSample\">Set objDialog = CreateObject(&#8220;UserAccounts.CommonDialog&#8221;)\nobjDialog.Filter = &#8220;VBScript Scripts|*.vbs|All Files|*.*&#8221;\nobjDialog.Flags = &amp;H0200\nobjDialog.FilterIndex = 1\nobjDialog.InitialDir = &#8220;C:\\Scripts&#8221;\nintResult = objDialog.ShowOpen\nIf intResult = 0 Then\n    Wscript.Quit\nElse\n    arrFiles = Split(objDialog.FileName, &#8221; &#8220;)\n    For i = 1 to Ubound(arrFiles)\n        strFile = arrFiles(0) &amp; arrFiles(i)\n        Wscript.Echo strFile\n    Next\nEnd If\n<\/PRE>\n<P>Here we\u2019re using the VBScript <B>Split<\/B> command to take the string we get back from the dialog box and make it into an array (by splitting on the blank space). The first element in the array will be the folder path; the remaining elements will be the individual file names.<\/P>\n<P>We then use a For Next loop to walk through the items in the array. Note that we start with the second item in the array, the item with the index number 1. (The first item in a VBScript array has an index number of 0.) We do that simply because the first item in the array isn\u2019t actually a file name; it\u2019s the folder path. Inside the For Next loop we combine the folder path (item 0) with the file name; that gives us a complete file path, which we store in a variable named strFile. We then echo this file path. <\/P>\n<P>When we run the script, we get output similar to this:<\/P><PRE class=\"codeSample\">C:\\Scripts\\file_dialog.vbs\nC:\\Scripts\\file_save.vbs\nC:\\Scripts\\test.vbs\n<\/PRE>\n<P>As soon as we have file paths we can actually do something interesting with those files, like open them, delete them, compress them, or whatever else you like to do with your files.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Awhile back you showed us how to use a File Open dialog box in our scripts. At the same time you said you\u2019d show us how to use a multi-select dialog box. Did you just forget about the multi-select dialog box?&#8212; AA Hey, AA. Yes, we \u2026 forgot \u2026 about the multi-select [&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-70333","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! Awhile back you showed us how to use a File Open dialog box in our scripts. At the same time you said you\u2019d show us how to use a multi-select dialog box. Did you just forget about the multi-select dialog box?&#8212; AA Hey, AA. Yes, we \u2026 forgot \u2026 about the multi-select [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70333","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=70333"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70333\/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=70333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}