DOS Batch Scripting

September 7, 2010 at 06:59:31
Specs: DOS
Hi ,
I am using
Microsoft Windows XP [Version 5.1.2600]
I am doing some small batch utility where user will be prompted either to Enter Yes or No.
Depending upon user input the control will be passed to another subroutine.

Utility is working fine as expected if user enters Yes or No.
If users enters some other input other than Yes or No,script is throwing error message as expected.
But error is not throwing if user just presses "Enter" key instead of Yes or No

SET /P repeat=If you want to do any more Upgrade or Rollback press "y" else "q" to quit:
if /i "%repeat%"=="y" cls &goto START
if /i "%repeat%"=="q" echo SCRIPT IS EXISTING& exit
echo.
if /i not "%repeat%"=="q" if /i not "%repeat%"=="y" echo PLEASE ENTER EITHER [Y]es or [Q]uit ONLY& goto EOF

Can any one help me how to avoid accepting "Enter" key from user or how to throw error when user presses "Enter" key??

Thanks in adavance,
Rao.


See More: DOS Batch Scripting

Report •


#1
September 7, 2010 at 12:22:29
I'd just get rid of the third if statement, we should only reach the last line if neither y or q match.

SET /P repeat=If you want to do any more Upgrade or Rollback press "y" else "q" to quit:
if /i "%repeat%"=="y" cls &goto START
if /i "%repeat%"=="q" echo SCRIPT IS EXISTING& exit
echo.
echo PLEASE ENTER EITHER [Y]es or [Q]uit ONLY
Pause
goto EOF


Report •

#2
September 7, 2010 at 13:15:36
Try this:

IF NOT DEFINED repeat EXIT

Can I put a question here?


Report •

#3
September 7, 2010 at 23:57:50
Thanks for the reply Viddywell and Matt123...

Viddywell...even though your suggestion is working but its not solving my problem...as this logic is only working for the first time...say the very first time the user press enter..this is working...after one iteration..this logic is not working.
is there any ways that "Enter key" can be stored in a variable and compared??

Matt123: Can you please explain more about how to use if not defined??

Thanks again guys..


Report •

Related Solutions

#4
September 8, 2010 at 01:27:38
I suggest a few things:

1. Clear the variable before using "set /p" - if the variable already has
something in it and the user simply hits enter the contents don't change.

2. Use "If not defined" (as suggested by Matt123)

3. Test if the variable is only double quotes.

4. Use a "for /f" as a conditional check for unwanted characters.

5. Remove any double quotes(they can screw with the if statements).

@echo off
:loop
set repeat=
SET /P repeat=If you want to do any more Upgrade or Rollback press "y" else "q" to quit:
if not defined repeat goto loop
if "%repeat:"=%"=="" goto loop

for /f "delims=qQyY" %%a in ("%repeat:"=%") do goto loop

set repeat=%repeat:"=%

if /i "%repeat%"=="y" cls &goto START
if /i "%repeat%"=="q" echo SCRIPT IS EXISTING& exit
goto loop

:start
pause

Edit - Clarity.


This should ensure that the user can enter *anything* but only allows,
"q", "y", or either with any number of double quotes.

e.g

y - pass
"y" - pass
"yq" -fail
q - pass
"q" - pass
"""y"" - pass


Report •

#5
September 8, 2010 at 02:46:35
Thanks a lot Judago...
Your suggestion solved my problem...its working very fine now...tons of thanks...
Could you please explain what does
1. "%repeat:"=%"==""
2. set repeat=%repeat:"=%
exactly will do....
that would be a great learning...

Thanks every one for your help guys...Long live computing.net :-)


Report •

#6
September 8, 2010 at 03:34:02
%repeat:"=%

The ':"=' part is telling the command processor to expand the variable replacing all occurrences of double quotes to nothing. Basic syntax is %var:string=replacement%, where "string" can't contain "=" and "replacement" can be nothing if required.

I suggest you start up cmd and type in "set /?", it explains it along with a few other things.

Stripping out the double quotes is important for standard %vars% because escaping works in blocks so a crafty input could cause it to crash or even execute a command ....


Report •

#7
September 8, 2010 at 03:46:39
I just realized that I was over complicating things......

Here is a much simpler version:

@echo off

:loop
set repeat=
SET /P repeat=If you want to do any more Upgrade or Rollback press "y" else "q" to quit:
if not defined repeat goto loop
if /i "%repeat:"=%"=="y" cls &goto START
if /i "%repeat:"=%"=="q" echo SCRIPT IS EXISTING& exit
goto loop

:start
pause


Report •

#8
September 8, 2010 at 03:47:57
Thanks a lot for your explanation and help tips...

Report •


Ask Question