Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9071423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:54:34+00:00 2026-06-16T17:54:34+00:00

I recently discovered a simply way to get user input in windows cmd.exe batch

  • 0

I recently discovered a simply way to get user input in windows cmd.exe batch files using the /p option provided by set command. But for a strange reason that I don’t understand the set /p behaves tricky when used between a if statement.

First, create a batch file named “ScriptRunsOK.bat” code below:

@echo off
rem Gets a User Yes-No Choice 
set /p UserInput=Your Choice [Y/N]:
set UserChoice=%UserInput:~0,1%
if "%UserChoice%"=="y" set UserChoice=Y
if "%UserChoice%"=="Y" (
    echo You Accepted[%UserChoice%] typing %UserChoice%
) else (
    echo You Rejected[%UserChoice%] typing %UserChoice%
)
pause

Second, create a batch file “ScriptRunsBAD.bat” enclosing the “User Input & Evaluation” code in a intentionaly only for example purpouses) between a if statement. Code as follows:

@echo off
rem Gets a User Yes-No Choice. Choice Nested in a IF Statement 
set DummyVar=OK
if "%DummyVar%"=="OK" (
    set /p UserInput=Your Chiuce [Y/N]:
    set UserChoice=%UserInput:~0,1%
    if "%UserChoice%"=="y" set UserChoice=Y
    if "%UserChoice%"=="Y" (
        echo You Accepted[%UserChoice%] typing %UserChoice%
    ) else (
        echo You Rejected[%UserChoice%] typing %UserChoice%
    )
)

pause

Third, run “ScriptRunsOK.bat” just double-clicking on it, or directly from de command line repeatedly. It works fine every time you run it. But if you try the same with “ScriptRunsBAD.bat”, it doesn’t work as I espected and furthermore, when you run it from command line it, strangely, preserves the user input typed on the previous excecution.

What has the code for “ScriptRunsBAD.bat” that causes the behavior? Is there an additional consideration I must have when using the set /p command within a if or multi-line statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:54:35+00:00Added an answer on June 16, 2026 at 5:54 pm

    Well. Finally, after reading the content of the links posted by @melpomene and @Wimmel (thanks) I could solve the issue. In short words (further details follow the links posted) cmd scripting evals (by default) variable evaluation when every line is parsed (not the habitual behavior expected when you are a programmer). The script solved uses SETLOCAL ENABLEDELAYEDEXPANSION to enable “Delayed Expansion” that allows that variables be expanded at execution time rather than at parse time when referenced using exclamation points (see !UserInput:~0,1! and !UserChoice!).

    The script solved:

    @echo off
    rem Gets a User Yes-No Choice. Choice Nested in a IF Statement 
    set DummyVar=OK
    SETLOCAL ENABLEDELAYEDEXPANSION
    if "%DummyVar%"=="OK" (
        set /p UserInput=Your Choice [Y/N]:
        set UserChoice=!UserInput:~0,1!
        if "!UserChoice!"=="y" set UserChoice=Y
        if "!UserChoice!"=="Y" (
            echo You Accepted[!UserChoice!] typing !UserInput!
        ) else (
            echo You Rejected[!UserChoice!] typing !UserInput!
        )
    )
    ENDLOCAL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently discovered you can compile JScript into console apps using a built in
I recently discovered that all Windows 7 machines have an handwriting recognition API within
We regularly setup simple winforms using Linq To Sql. I recently discovered that the
I recently discovered this little scala example called Simple interpreter using monads : object
I recently discovered the Enterprise Validation Library and am using it to validate my
Although I've been using Ruby 1.9 for a while, I only recently discovered the
I recently discovered our ink-enabled-application didn't run. It was a Windows (vista or 7)
I recently discovered that it is possible to write windows applications in php. Can
Recently discovered the convenience of using MySQL Views to accumulate data for presentation. I
I recently discovered Erlang and am now working my way through a couple of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.