Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: set /p is not taking input from keyboard


17 Mar 2011   #1

win7 pro
 
 
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)


My System SpecsSystem Spec
.

17 Mar 2011   #2

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
17 Mar 2011   #3

win7 pro
 
 

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 Windows 7. 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 System SpecsSystem Spec
.


17 Mar 2011   #4

Microsoft Community Contributor Award Recipient

Vista x64 / 7 X64
 
 

I usually have a space there


set /P UsersChoice= Overwrite before continue etc........whatever:
My System SpecsSystem Spec
17 Mar 2011   #5

win7 pro
 
 

Quote   Quote: Originally Posted by SIW2 View Post
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 Windows 7 ???
My System SpecsSystem Spec
17 Mar 2011   #6

Windows 8.1 Pro (x64)
 
 

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 System SpecsSystem Spec
17 Mar 2011   #7

XP / Win7 x64 Pro
 
 

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 System SpecsSystem Spec
18 Mar 2011   #8

win7 pro
 
 

Quote:
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.

Quote:
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 Windows 7 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 Windows 7 in VB. That works pretty nice and we use VB environment to test out new tools.
My System SpecsSystem Spec
18 Mar 2011   #9

Windows 8.1 Pro (x64)
 
 

Quote   Quote: Originally Posted by zainka View Post
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 System SpecsSystem Spec
18 Mar 2011   #10

Windows 8.1 Pro RTM x64
 
 

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 System SpecsSystem Spec
Reply

 set /p is not taking input from keyboard




Thread Tools



Similar help and support threads for2: set /p is not taking input from keyboard
Thread Forum
Keyboard and input language Hardware & Devices
Log on frozen, Does not allow keyboard input. System Security
Keyboard-input problem General Discussion
Keyboard and mouse input errors Hardware & Devices
Chinese Wubi 86 input keyboard Installation & Setup
Problem with keyboard input Hardware & Devices
Issue with keyboard input Hardware & Devices

Our Sites

Site Links

About Us

Find 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 09:05 AM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33