{"id":101,"date":"2007-03-29T09:00:00","date_gmt":"2007-03-29T17:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/03\/29\/more-fun-with-choice\/"},"modified":"2007-03-29T09:00:00","modified_gmt":"2007-03-29T17:00:00","slug":"more-fun-with-choice","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/03\/29\/more-fun-with-choice\/","title":{"rendered":"More fun with CHOICE"},"content":{"rendered":"<p>Let&#8217;s continue to look at CHOICE.EXE and how to use it in a batch file. Here&#8217;s what you might have for a command line menu system of common admin tasks:<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">@AdminMenu.bat<br \/><\/font><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">GOTO :MENU <br \/><\/font><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">:MENU<\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">cls<br \/><\/font><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">echo Admin Menu<br \/>echo 1 &#8211; Create User<br \/>echo 2 &#8211; Create Group<br \/>echo 3 &#8211; Create Computer<br \/>echo 4 &#8211; Quit<br \/>Choice \/C 1234 \/M &#8220;Enter a menu selection&#8221; <\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">if errorlevel 4 GOTO :EOF<br \/>if errorlevel 3 GOTO :COMPUTER<br \/>if errorlevel 2 GOTO :GROUP<br \/>if errorlevel 1 GOTO :USER<br \/>GOTO :EOF <\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">:USER<br \/>ECHO Create New user<\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">PAUSE<br \/>goto :MENU<\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">:GROUP<br \/>ECHO Create New Group<\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">PAUSE<br \/>goto :MENU <\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">:COMPUTER <\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">ECHO Create New computer<\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">PAUSE<br \/>goto :MENU<\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">:EOF<\/font> <\/p>\n<p>I&#8217;m echoing a menu to the screen and then using CHOICE to prompt the user to make a menu selection. The \/C switch lets met specify the choice options and \/M will be the message prompt. When run, the user should see something like this: <\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">Admin Menu<br \/>1 &#8211; Create User<br \/>2 &#8211; Create Group<br \/>3 &#8211; Create Computer<br \/>4 &#8211; Quit<br \/>Enter a menu selection [1,2,3,4]?<\/font> <\/p>\n<p>The cursor will wait at the prompt until the user makes a selection of 1,2,3 or 4. Any other selection will cause the computer to complain with a beep. The only other way to abort is to press Ctrl-C and break out of the script so you should always give the user an option to gracefully exit your script. <\/p>\n<p>As I discussed last time, the selected option will correspond to an Errorlevel value so all I have to do is jump to the right section of the script: <\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">if errorlevel 4 GOTO :EOF<br \/>if errorlevel 3 GOTO :COMPUTER<br \/>if errorlevel 2 GOTO :GROUP<br \/>if errorlevel 1 GOTO :USER<br \/>GOTO :EOF <\/font><\/p>\n<p>I probably don&#8217;t need the last GOTO :EOF statement since the user can only enter the choices I&#8217;ve made or CHOICE will beep and not move on. Still, I like including this to cover the unexpected so that the script will gracefully exit. It doesn&#8217;t cost me anything to keep it in.<\/p>\n<p>Within each section, I just have placeholder code to indicate what each section will do. The Pause command waits for the user to press any key, and then script execution returns to the main menu.<\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">:COMPUTER <br \/><\/font><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">ECHO Create New computer<\/font><\/p>\n<p><font style=\"color: #0000ff\" face=\"Lucida Console\" color=\"#0000ff\" size=\"2\">PAUSE<br \/>goto :MENU<\/font><\/p>\n<p>I&#8217;ve got one more entry on this topic so stay tuned.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:f1c03d85-50f8-423d-bdc8-d75b70717869\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\"><span class=\"sizeLess20\">Technorati tags: <\/span><a href=\"http:\/\/technorati.com\/tags\/CMD\" rel=\"tag\"><span class=\"sizeLess20\">CMD<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/technorati.com\/tags\/Batch\" rel=\"tag\"><span class=\"sizeLess20\">Batch<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/technorati.com\/tags\/Choice\" rel=\"tag\"><span class=\"sizeLess20\">Choice<\/span><\/a><span class=\"sizeLess20\">, <\/span><a href=\"http:\/\/technorati.com\/tags\/Commandline\" rel=\"tag\"><span class=\"sizeLess20\">Commandline<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s continue to look at CHOICE.EXE and how to use it in a batch file. Here&#8217;s what you might have for a command line menu system of common admin tasks:<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-batchcmd"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}