Jump to content

BAT CMD - Y/N input without pressing enter?


Recommended Posts

Is this possible? The only info I found was here, the first script doesn't work and the second makes no sense:

https://forums.windowssecrets.com/showthread.php/113953-Yes-No-Batch-file-(XP-SP2)

I would like to create a CMD that prompts user y / n question with goto in place. But automatically continue as soon as input is detected (contrast to waiting for another ENTER press to finish).

As it stands:

@echo Log off + log in to complete installation!
SET /P _inputname= Log Off now?:
IF "%_inputname%"=="Y" GOTO :Y
GOTO :end
:Y
cmd.exe /c c:\windows\system32\logoff.exe
:end

 

Edited by shorterxp
Link to comment
Share on other sites


You need choice or similar (there are many of such replacements), for some strange reasons the good MS guys removed it from NT4, 2K and XP (it is in the Resource kit) but re-added it in Vista.

*like*:

http://www.donationcoder.com/forum/index.php?topic=31344.0

http://www.robvanderwoude.com/choice.php

In the latter there is a link to "workaround" in pure batch, secondchoice:

http://www.robvanderwoude.com/batexamples.php?fc=S#SecondChoice

http://www.robvanderwoude.com/sourcecode.php?src=secondchoice_xp

Or you can use this clever trick making use of XCOPY:

https://www.dostips.com/forum/viewtopic.php?t=5409

jaclaz

 

Edited by jaclaz
Link to comment
Share on other sites

Forbidden

You don't have permission to access /board/topic/177670-bat-cmd-yn-input-without-pressing-enter/ on this server.

 

ARGGHHHHHHHHHHHHH not this shTYEt again!!!

Edited by shorterxp
Link to comment
Share on other sites

19 hours ago, shorterxp said:

Forbidden

You don't have permission to access /board/topic/177670-bat-cmd-yn-input-without-pressing-enter/ on this server.

 

ARGGHHHHHHHHHHHHH not this shTYEt again!!!

Well, it seemingly went through somehow. :unsure:

jaclaz

Link to comment
Share on other sites

Why not use VBS to do what you want. This way you do not have

3rd party apps to do what you want.

  
If MsgBox("Would you like to restart the Computer?",4132,_
"Shut Off Computer") = 6 Then
'-> Code Below Here Yes
 MsgBox "User said yes to restart"
Else
'-> Code Below Here No 
 MsgBox "User said no to restart"
End If 

 

Link to comment
Share on other sites

Hi, thanks. Theres a bug in forum ("forbidden") shows up when try to use 'code snippet' or 'quote' feature, so I've resorted to using screenshot.

The example on robvanderwoude.com did not work.

The example on wikpedia did work. See below.

untitled.png

 

Edited by shorterxp
Link to comment
Share on other sites

On 8/6/2018 at 7:47 PM, shorterxp said:

Hi, thanks. Theres a bug in forum ("forbidden") shows up when try to use 'code snippet' or 'quote' feature, so I've resorted to using screenshot.

The example on robvanderwoude.com did not work.

The example on wikpedia did work. See below.

untitled.png

 

Well that uses CHOICE.

I wonder if we are communicating :dubbio:

jaclaz

Link to comment
Share on other sites

I could have googled that myself :dow:

To clarify:

robvanderwoude.com the choice example, using choice.exe did not work as desired.

The example on wikipedia, also ussing choice.exe, did work.

Edited by shorterxp
Link to comment
Share on other sites

I understand that. :)

Point I was trying to make - since your nick is shorterxp and you are using seemingly XP - is that choice.exe is NOT available in XP (nor it is on NT 4.0 or 2K) as it has been re-added to the OS starting from Vista.

So, if you want to create a "portable" .cmd making use of choice.exe you need to (you choose):

1) limit its use to Vista and later system 
2) add to the batch a copy of choice.exe or - in case of redistribution, since MS choice.exe is NOT redistributable - add one of the free replacements for it
3) use a "built-in" replacement/trick/workaround

I checked and the robvanderwoude.com does work BUT it does not provide one of your requests (choosing with a single keypress without pressing ENTER).

The other suggested workaround BOTH works and provides the feature you asked for:

https://www.dostips.com/forum/viewtopic.php?t=5409

basically:

for /F "delims=" %%L in ('xcopy /L /w "%~f0" "%~f0" 2^>NUL') do (
if not defined key set "key=%%L"
)

a more complete example:

@ECHO OFF
SET mykey=
:myChoice
SET myChoice=A suffusion of yellow
ECHO Please Choose:
ECHO Press 1 for Option 1
ECHO Press 2 for Option 3
ECHO Press A for Option Z :w00t:
ECHO Press Enter to choose to not choose
for /F "delims=" %%L in ('xcopy /L /w "%~f0" "%~f0" 2^>NUL') do (
if not defined mykey set "mykey=%%L"
)

SET mykey=%mykey:~-1,1%
IF NOT DEFINED mykey (ECHO You pressed [Enter]) ELSE (SET myChoice=%mykey%)
ECHO You chose: %myChoice%

jaclaz

Link to comment
Share on other sites

I think you could use a similar method, instead using replace.exe from a for loop:

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion

:Loop
Set "#="
Set /P "=Please enter Y or N :"<Nul
For /F Skip^=1^ Delims^=^ EOL^= %%$ In ('Replace ? . /U /W') Do If Not Defined # Set "#=%%$"
Echo(
If /I "%#%"=="Y" GoTo Entrey
If /I "%#%"=="N" GoTo Entren
GoTo Loop

:Entrey
Echo You entered Y & Pause
GoTo :EOF

:Entren
Echo You entered N & Pause
GoTo :EOF

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...