Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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