set /p is not taking input from keyboard

zainka

New member
Local time
10:44 AM
Messages
5
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:

My Computer My Computer

OS
win7 pro
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 My Computer

OS
Windows 7 Ultimate x64
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:
[B]Syntax[/B]
[B]set[/B] [[B]/p[/B] [[I]variable[/I][B]=[/B]]] [I]string[/I]]

where:
[B]/p[/B][B]: [/B]Sets the value of [I]variable[/I] to a line of input. 
[B][I]variable[/I][/B][B]: [/B]Specifies the variable you want to set or modify. 
[B][I]string[/I][/B][B]: [/B]Specifies the string you want to associate with the specified variable.
Thanks anyway.

Breg
Vidar (Z)
 

My Computer My Computer

OS
win7 pro
I usually have a space there


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

My Computers My Computers

System One System Two

  • Computer type
    PC/Desktop
    OS
    7 X64
    CPU
    i5 8400
    Motherboard
    gigabyte b365m ds3h
    Memory
    2x8gb 3200mhz
    Hard Drives
    various
    PSU
    pure power 11 400w cm
    Case
    Coolermaster
    Cooling
    cryorig m9i
  • Computer type
    PC/Desktop
    OS
    7x64
    CPU
    g5400
    Motherboard
    ga b365m ds3h
    Memory
    8gb ddr4 2400
    PSU
    xfx pro 450w
I usually have a space there

Tried it, no change. However, its not an requirement. In ..<cough>... Linux :rolleyes: 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 My Computer

OS
win7 pro
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 My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Alienware Aurora ALX R4
OS
Windows 10 Pro (x64)
CPU
Intel Core i7-3930K (3.2GHz - 4.5GHz)
Motherboard
Alienware Aurora-R4 x79
Memory
4x Samsung 4GB PC3-12800 DDR3 (16GB 1600MHz)
Graphics Card(s)
Nvidia Geforce GTX 690
Sound Card
SteelSeries Siberia Elite
Monitor(s) Displays
Dell UltraSharp U3011
Screen Resolution
2560x1600
Hard Drives
Samsung 850 Pro 256 GB, Seagate 1TB Desktop Hybrid HDD, 2x Western Digital 4TB Green HDD
PSU
875W Some Dell PSU <.<
Case
Alienware Aurora ALX
Cooling
Custom Liquid Cooling (EK CPU & GPU blocks) dual EK 480RAD
Keyboard
Logitech G710+ Mechanical
Mouse
Logitech G700s
Internet Speed
Verizon Fios (50 mbps average)
Other Info
Server: Intel NUC D54250WYK: i5-4250U, 16GB, 256 GB mSATA, Windows Server 2012 R2
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 My Computer

OS
XP / Win7 x64 Pro
CPU
Intel Quad-Core Q9450 @ 3.2GHz
Motherboard
Asus P5-E
Memory
2x2GB GSkill DDR2
Graphics Card(s)
NVIDIA GeForce 8600 GTS (EVGA)
Monitor(s) Displays
Dell 2408WFP
Screen Resolution
1920x1200
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:

My Computer My Computer

OS
win7 pro
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...get-user-input-more-nicely-in-powershell.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 My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Alienware Aurora ALX R4
OS
Windows 10 Pro (x64)
CPU
Intel Core i7-3930K (3.2GHz - 4.5GHz)
Motherboard
Alienware Aurora-R4 x79
Memory
4x Samsung 4GB PC3-12800 DDR3 (16GB 1600MHz)
Graphics Card(s)
Nvidia Geforce GTX 690
Sound Card
SteelSeries Siberia Elite
Monitor(s) Displays
Dell UltraSharp U3011
Screen Resolution
2560x1600
Hard Drives
Samsung 850 Pro 256 GB, Seagate 1TB Desktop Hybrid HDD, 2x Western Digital 4TB Green HDD
PSU
875W Some Dell PSU <.<
Case
Alienware Aurora ALX
Cooling
Custom Liquid Cooling (EK CPU & GPU blocks) dual EK 480RAD
Keyboard
Logitech G710+ Mechanical
Mouse
Logitech G700s
Internet Speed
Verizon Fios (50 mbps average)
Other Info
Server: Intel NUC D54250WYK: i5-4250U, 16GB, 256 GB mSATA, Windows Server 2012 R2
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 My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dwarf Dwf/11/2012 r09/2013
OS
Windows 8.1 Pro RTM x64
CPU
Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.4GHz)
Motherboard
ASRock Z77 Extreme4-M
Memory
4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
Graphics Card(s)
MSI GeForce GTX770 Gaming OC 2GB
Sound Card
Realtek High Definition on board solution (ALC 898)
Monitor(s) Displays
ViewSonic VA1912w Widescreen (VGA)
Screen Resolution
1440x900
Hard Drives
OCZ Agility 3 SSD 120GB SATA III x2 (RAID 0)
Samsung HD501LJ 500GB SATA II x2
Hitachi HDS721010CLA332 1TB SATA II
Iomega 1.5TB Ext USB 2.0
WD 2.0TB Ext USB 3.0
PSU
XFX Pro Series 850W Semi-Modular
Case
Gigabyte IF233
Cooling
1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
Keyboard
Microsoft Comfort Curve Keyboard 3000 (USB)
Mouse
Microsoft Comfort Mouse 3000 for Business (USB)
Internet Speed
NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
Antivirus
Avast! 8.0.1497
Browser
IE 11
Other Info
Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
Lexmark S305 Printer/Scanner/Copier (USB)
WEI Score: 8.1/8.1/8.5/8.5/8.25
Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
Code:
That is not PowerShell. "set /p" is not a PowerShell command.
Good, then I don’t have to worry about power shell.

@Dwarf.
Thanks for your input, but the problem is related to "set /p" and in your script it remains the same. I will use the /I switch though.

I start to believe problem IS related to WM box and Ill have to wait until some real machines is deployed.

Thanks to all who have participated.
 

My Computer My Computer

OS
win7 pro
I tested my code thoroughly, and couldn't find any bugs in it. In what way isn't it working for you?
 

My Computer My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dwarf Dwf/11/2012 r09/2013
OS
Windows 8.1 Pro RTM x64
CPU
Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.4GHz)
Motherboard
ASRock Z77 Extreme4-M
Memory
4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
Graphics Card(s)
MSI GeForce GTX770 Gaming OC 2GB
Sound Card
Realtek High Definition on board solution (ALC 898)
Monitor(s) Displays
ViewSonic VA1912w Widescreen (VGA)
Screen Resolution
1440x900
Hard Drives
OCZ Agility 3 SSD 120GB SATA III x2 (RAID 0)
Samsung HD501LJ 500GB SATA II x2
Hitachi HDS721010CLA332 1TB SATA II
Iomega 1.5TB Ext USB 2.0
WD 2.0TB Ext USB 3.0
PSU
XFX Pro Series 850W Semi-Modular
Case
Gigabyte IF233
Cooling
1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
Keyboard
Microsoft Comfort Curve Keyboard 3000 (USB)
Mouse
Microsoft Comfort Mouse 3000 for Business (USB)
Internet Speed
NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
Antivirus
Avast! 8.0.1497
Browser
IE 11
Other Info
Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
Lexmark S305 Printer/Scanner/Copier (USB)
WEI Score: 8.1/8.1/8.5/8.5/8.25
Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
I tested my code thoroughly, and couldn't find any bugs in it. In what way isn't it working for you?

It's not a matter of whether the code is right or not. His original code works fine on my machine and should work on everyone else's as well. I believe the problem stems from him being in a virtual environment but I'm not sure why that changes anything.

zainka, did you install the Guest Additions in Virtual Box after you got the VM up and running? If not, or even if you did, I would try doing so again, just in case that somehow got screwed up.

virtualbox-install-guest-additions.jpg
 

My Computer My Computer

OS
XP / Win7 x64 Pro
CPU
Intel Quad-Core Q9450 @ 3.2GHz
Motherboard
Asus P5-E
Memory
2x2GB GSkill DDR2
Graphics Card(s)
NVIDIA GeForce 8600 GTS (EVGA)
Monitor(s) Displays
Dell 2408WFP
Screen Resolution
1920x1200
Back
Top