set /p is not taking input from keyboard

Page 1 of 2 12 LastLast

  1. Posts : 5
    win7 pro
       #1

    set /p is not taking input from keyboard


    Hi I have been struggling to have "set /p" to work correctly in a batchfile. Its purpose is to print a text and ask for user input from keyboard and store this is in a variable.
    Here is my code

    Code:
    1  if not exist %MY_FILE% (
    2    :CopyFile
    3    echo copying
    4    copy /y %MY_FILE% %NEW_DIRECTORY% >NUL ) else (
    5      echo %MY_FILE% already exist 
    6      set UsersChoice=
    7      set /P UsersChoice=Overwrite before continue, Continue with old, Abort? o/c/a:
    8      echo input was %UsersChoice%
    9      if "%UsersChoice%"=="o" (
    10        echo overwrite
    11        goto :CopyFile ) else if "%UsersChoice%"=="c" (
    12          echo continue
    13          goto :Continue ) else (
    14            echo exit))
    15
    16  :Continue
    Code is showing the text and is waiting for input but whatever i type before hit ENTER adds no value to UsersChoice. echo input was %UserChoice% only shows "input was " and nothing more. Then the if/else will be evaluated to else in line 13 printing exit (when working it will simply just exit)

    Why are there no input stored in UsersChoice ???

    This is the shell output where I have entered "o" as my choice (seen after : ).
    Code:
    Overwrite before continue, Continue with old, Abort? o/c/a:o
    input was
    exit
    Best regards
    Vidar (Z)
    Last edited by zainka; 17 Mar 2011 at 06:43. Reason: typo
      My Computer


  2. Posts : 3
    Windows 7 Ultimate x64
       #2

    Code:
    set /P UsersChoice=Overwrite before continue, Continue with old, Abort? o/c/a:
    I believe it should be
    Code:
    set /P UsersChoice=Overwrite before continue, Continue with old, Abort? o/c/a:=
      My Computer


  3. Posts : 5
    win7 pro
    Thread Starter
       #3

    I have tried both with = and %=% as I have seen it used some time, but behavior does not change. Also notice that the documentations does not mention the use of any extra =.

    Also, removing the prompt string, which is optional, does not give any solutions.

    This is snipped from WinXP product documentation, but I guess the set command remains unchanged in win7. At least help shows no difference.
    Code:
    Syntax
    set [/p [variable=]] string]
    
    where:
    /p: Sets the value of variable to a line of input. 
    variable: Specifies the variable you want to set or modify. 
    string: Specifies the string you want to associate with the specified variable.
    Thanks anyway.

    Breg
    Vidar (Z)
      My Computer


  4. Posts : 16,129
    7 X64
       #4

    I usually have a space there


    set /P UsersChoice= Overwrite before continue etc........whatever:
      My Computers


  5. Posts : 5
    win7 pro
    Thread Starter
       #5

    SIW2 said:
    I usually have a space there
    Tried it, no change. However, its not an requirement. In ..<cough>... Linux one can run a script in debug mode and cinda follow its step. I believe one also have te ability to request more debug prints from system while running a script. Are there any such possibilities in win7 ???
      My Computer


  6. Posts : 5,642
    Windows 10 Pro (x64)
       #6

    Have you thought of moving to the more powerful environment that is PowerShell?

    Windows 7 even comes with a developer program for creating PowerShell scripts.
    With debugging and all that jazz. Along with tab completion for commands and params.
      My Computer


  7. Posts : 1,814
    XP / Win7 x64 Pro
       #7

    What are you using to edit the batch file and how are you running it?

    The script above works fine for me in just creating the .bat file and running it from command prompt. Of course, I added "@echo off" at the beginning to clean up the output.
      My Computer


  8. Posts : 5
    win7 pro
    Thread Starter
       #8

    What are you using to edit the batch file and how are you running it?

    The script above works fine for me in just creating the .bat file and running it from command prompt. Of course, I added "@echo off" at the beginning to clean up the output.
    I am using textpad. from helios (www.textpad.com).

    The script is a part of a bigger script so I have @echo off as well. I also tried to isolate the problem to have only the shown part in a single script as you did above, just to make sure nothing else is cluttering the script, but same behaviour. And... if MY_FILE does not exist the script just copy it and continues as it should. It is only when trying to read input from keyboard when MY_FILE do exist I have encountered a problem.

    Have you thought of moving to the more powerful environment that is PowerShell?
    Even when running PS, script wont take input from "set /p"

    However. I thought I had mentioned that I was using w7 in Oracle Virtual box, I noticed I had not. Could the keyboard input to VB be altered somehow? I find it very unlikely as there is no other signs of problems running w7 in VB. That works pretty nice and we use VB environment to test out new tools.
    Last edited by zainka; 18 Mar 2011 at 03:23. Reason: typos
      My Computer


  9. Posts : 5,642
    Windows 10 Pro (x64)
       #9

    zainka said:
    Even when running PS, script wont take input from "set /p"
    That is not PowerShell. "set /p" is not a PowerShell command.

    Code:
    PS > $var = Read-Host "What is your name?"
    What is your name?: Logic
    PS > $var
    Logic
    That is PowerShell.

    Or even: http://blogs.technet.com/b/jamesone/...owershell.aspx
    Code:
    PS > $enabled=[boolean](select-item -Caption "Configuring RemoteDesktop" -Message "Do you want to: " -choice "&Disable Remote Desktop", "&Enable Remote Desktop" -default 1 ) 
    
    Configuring RemoteDesktop 
    Do you want to: 
    [D] Disable Remote Desktop  [E] Enable Remote Desktop  [?] Help (default is "E"): d 
    
    
    PS > $enabled 
    False
    The above avoids human error.
      My Computer


  10. Posts : 9,582
    Windows 8.1 Pro RTM x64
       #10

    Try the following:
    Code:
    :Main
    @echo off
    set UsersChoice=
    set /P UsersChoice=Overwrite before continue, Continue with old, Abort? o/c/a:
    echo Response was %UsersChoice%
    REM note that the following statement is all on the same line
    REM the /I switch allows the use of upper and lower case characters
    REM the routine goes back to the beginning if an invalid character is entered
    if /I "%UsersChoice%"=="o" ( goto :CopyFile ) else if /I "%UsersChoice%"=="c" ( goto :Continue ) else if /I "%UsersChoice%"=="a" ( goto :Abort ) else goto :Main
    pause
    :CopyFile
    echo Overwite
    REM overwrite routine goes here
    goto :exit
    :Continue
    echo Continue
    REM continue routine goes here
    goto :exit
    :Abort
    echo Abort
    :exit
    pause
    You can omit the REM lines if you wish.

    Hope this helps.
      My Computer


 
Page 1 of 2 12 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 11:57.
Find Us