Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Writing a batch file compatible with XP


  • Please log in to reply

#1
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
Hi, I'm trying to write a batch file that runs under XP. Here it is (slightly modified to take out long bits of unimportant echoing)

@echo off
echo Welcome
echo Press Y or N
choice /C:YN
if errorlevel 1 goto yes
if errorlevel 2 goto no
:yes
echo You pressed Y!
pause
goto quit
:no
echo You pressed N!
pause
goto quit
:quit
exit

Okay, now at first the "choice" wouldn't work at all, so I downloaded CHOICE.COM from Microsoft, and now it works. However, no matter whether I press Y or N, it goes to "yes"! Why won't it go to "no"?

Thanks,
stettybet0
  • 0

Advertisements


#2
Neil Jones

Neil Jones

    Member 5k

  • Member
  • PipPipPipPipPipPipPipPip
  • 8,476 posts
Errorlevel values need to be checked in reverse order, more so under XP than any other version of DOS.

The safest way to use errorlevels for all DOS versions is the reverse order check.
Start checking the highest errorlevel that can be expected, then check for the one below, etcetera:

IF ERRORLEVEL 255 GOTO Label255
IF ERRORLEVEL 254 GOTO Label254

	*
	*
	*

IF ERRORLEVEL 2 GOTO Label2
IF ERRORLEVEL 1 GOTO Label1
GOTO Label0 

:Label255
(commands to be executed at errorlevel 255)
GOTO End

	*
	*
	*

:Label1
(commands to be executed at errorlevel 1)
GOTO End

:Label0
(commands to be executed at errorlevel 0, or no errorlevel)

:End
This will result in many more lines of batch code, but at least it will work in any DOS version.


See http://www.robvander...errorlevel.html for more details.
  • 0

#3
stettybet0

stettybet0

    Trusted Tech

  • Topic Starter
  • Technician
  • 2,579 posts
Thanks! That worked.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP