As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Joptionpain for cmd.exe

oniianoniian Registered User regular
edited May 2007 in Help / Advice Forum
I have been scouring the web for a way to implement something like JoptionPain in Java. I have a command prompt program I have setup that I want to prompt the user to respond to whether they want to proceed or cancel, killing the window. I don't care whether it asks the user in cmd.exe or in a seperate window that pops up. How do I make cmd.exe interactive with the user running the command script?

oniian on

Posts

  • Options
    aperlscriptaperlscript Registered User regular
    edited May 2007
    You can use 'set' in your batchfile with a /p switch:

    set /p answer=Is cmd.exe the most poorly documented shell ever?
    if /i "%answer%" == "y" (
    echo That is correct!
    ) else (
    echo WRONG!
    )

    aperlscript on
  • Options
    oniianoniian Registered User regular
    edited May 2007
    Thanks a bunch, I have been looking for documentation on how to script an interactive prompt for user for sometime in little chunks but couldn't find an resource that talked about coding in cmd. Just out of curiousity where did you pick that up and is there any way to hide the inital coding in the prompt so it doesn't show up in the prompt before the question is asked?

    oniian on
  • Options
    aperlscriptaperlscript Registered User regular
    edited May 2007
    oniian wrote: »
    Just out of curiousity where did you pick that up
    I picked it up from looking at other batchfiles. Check out "cmd /?", there's a lot of starting points there. In particular, there's a list of native commands (including "for", "if", and other programming things) that all support usage info via a "/?" switch. Bring your patience hat. Google also returns some decent batch tutorials.
    oniian wrote: »
    and is there any way to hide the inital coding in the prompt so it doesn't show up in the prompt before the question is asked?
    Two ways:

    You can hide any line in a batchfile by having "@" be the first non-whitespace character:

    echo This will look funny
    @echo This will look cleaner

    The second is to put 2 lines at the top:

    @setlocal
    @echo off

    "off" is a special argument to echo to tell all the subsequent commands to not print before executing. "setlocal" is a scoping thing, the details are kinda complicated but it's a good habit to get into if you're planning on doing more complicated batchfiles, especially if you're calling into other batchfiles. For your purposes, it is only applying the "echo off" to your batchfile (among other things, but read the usage for those details).

    aperlscript on
Sign In or Register to comment.