Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


Sign in to follow this  
shorterxp

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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 

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

use echo like this

echo ramdisk| convert f: /fs:ntfs /x

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
14 hours ago, shorterxp said:

echo ramdisk| convert f: /fs:ntfs /x

 

That formats hard drive?

No.

At the most (not as posted) it will convert a volume (F: in this case) to NTFS.

https://ss64.com/nt/convert.html

jaclaz

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...