{"id":85940,"date":"2004-08-31T12:41:28","date_gmt":"2004-08-31T20:41:28","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=85940"},"modified":"2019-06-03T12:43:22","modified_gmt":"2019-06-03T20:43:22","slug":"how-can-i-prompt-a-user-for-input","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-prompt-a-user-for-input\/","title":{"rendered":"How Can I Prompt a User for Input?"},"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! I need my script to prompt users to enter some information, like the name of the file they want to create. How do I do that?<BR><BR>&#8212; RW, Williamsport, PA<\/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, BN. The simplest &#8211; and most foolproof &#8211; way to do this is to use an InputBox; when you do that users will be presented with a graphical dialog box similar to this:<\/P>\n<P><IMG border=0 alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/inputbox.jpg\" width=363 height=160><\/P>\n<P>Displaying the dialog box is easy; you simply call the InputBox function, passing that function two parameters:<\/P>\n<TABLE id=E4C class=dataTable cellSpacing=0 cellPadding=0 class=\"dataTable\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=record vAlign=top>\n<TD>\n<P class=lastInCell><B>The message to be displayed.<\/B><\/P><\/TD>\n<TD style=\"BORDER-RIGHT: #cccccc 1px solid\">\n<P class=lastInCell>In this example, the message is <I>Please enter a name for your new file:<\/I>.<\/P><\/TD><\/TR>\n<TR class=evenRecord vAlign=top>\n<TD>\n<P class=lastInCell><B>The caption for the dialog box.<\/B><\/P><\/TD>\n<TD style=\"BORDER-RIGHT: #cccccc 1px solid\">\n<P class=lastInCell>In this example, the caption is <I>Create File<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=dataTableBottomMargin><\/DIV>\n<P>The code itself looks something like this:<\/P><PRE class=codeSample>strAnswer = InputBox(&#8220;Please enter a name for your new file:&#8221;, _\n    &#8220;Create File&#8221;)\n<\/PRE>\n<P>Note that the variable strAnswer will be used to store whatever the user types into the dialog box.<\/P>\n<P>So far so good. Now, how do you figure out what file name the user typed in? That\u2019s easy; like we said, the value the user enters is automatically stored in the variable strAnswer. Thus:<\/P><PRE class=codeSample>strAnswer = InputBox(&#8220;Please enter a name for your new file:&#8221;, _\n    &#8220;Create File&#8221;)\nWscript.Echo strAnswer\n<\/PRE>\n<P>There\u2019s only one complication here. As you can see, the dialog box has both an OK button and a Cancel button. The idea is that the user types in a file name and then clicks OK. But what happens if the user clicks Cancel? Theoretically, that means that the user doesn\u2019t want to create a file after all; because of that, the script should either terminate or move on to some other task. <\/P>\n<P>Fortunately, this a case where theory can easily be put into practice. Any time a user clicks the Cancel button, strAnswer is set to an empty string (e.g., strAnswer = \u201c\u201d). To determine whether the user clicked Cancel (or, alternatively, clicked OK without entering a file name), all we have to do is check to see if strAnswer equals an empty string. This modified script checks the value of strAnswer; if strAnswer is empty the script terminates (using Wscript.Quit). Otherwise, the script echoes the value the user typed into the dialog box:<\/P><PRE class=codeSample>strAnswer = InputBox(&#8220;Please enter a name for your new file:&#8221;, _\n    &#8220;Create File&#8221;)\nIf strAnswer = &#8220;&#8221; Then\n    Wscript.Quit\nElse\n    Wscript.Echo strAnswer\nEnd If\n<\/PRE>\n<P>Here\u2019s a variation which places the InputBox in a loop. If the user clicks Cancel, the script reminds the user to enter a file name, and then displays the InputBox again. As soon as the user enters a file name and clicks OK, the script displays the value entered and then exits the loop (Exit Do):<\/P><PRE class=codeSample>Do While X = 0\n    strAnswer = InputBox _\n        (&#8220;Please enter a name for your new file:&#8221;,&#8221;Create File&#8221;)\n    If strAnswer = &#8220;&#8221; Then\n        Wscript.Echo &#8220;You must enter a file name.&#8221;\n    Else\n        Wscript.Echo strAnswer\n        Exit Do\n    End If\nLoop\n<\/PRE>\n<P>For more information about the Input function, see the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/script56\/html\/vsfctInputBox.asp\" target=_blank>VBScript documentation<\/A> on MSDN.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need my script to prompt users to enter some information, like the name of the file they want to create. How do I do that?&#8212; RW, Williamsport, PA Hey, BN. The simplest &#8211; and most foolproof &#8211; way to do this is to use an InputBox; when you do that users [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,3,45],"class_list":["post-85940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need my script to prompt users to enter some information, like the name of the file they want to create. How do I do that?&#8212; RW, Williamsport, PA Hey, BN. The simplest &#8211; and most foolproof &#8211; way to do this is to use an InputBox; when you do that users [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/85940","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=85940"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/85940\/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=85940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=85940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=85940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}