Need batch file help - choice

JTG February 5, 2009 at 12:54:49
Specs: Windows XP
I am trying to do a bat file with choices of yes and no. This is what I did, but it is not working correctly. Any help would be appreciated.

CHOICE /c:yn /t:y,30
echo Delete all files?
echo (Y) Yes, delete all files in folder
echo (N) No, Do not delete all files in folder


ECHO Deleting files.
del "%drive%"

ECHO Files not deleted. Exiting program ....


See More: Need batch file help - choice

Report •

February 5, 2009 at 13:29:43
Choice doesn't set a variable it sets an errorlevel.

The errorlevel set is in relation to the characters position after /c:, the errorlevels will count up from left to right - /c:123456789.....

Since your using xp you can just use "if %errorlevel%==number <stuff>". You can also do things the old way, especially if you want the script to work in dos based systems. The thing to rember doing the errorelevel check is that you start from the highest number and work down.

if errorlevel 3 goto x 
if errorlevel 2 goto y
if errorlevel 1 goto z

In your situation it would be:

if errorlevel 2 goto exit
if errorlevel 1 goto delete

Forgive me if I have made any mistakes, this is from memory, I am currently on a linux box.

Report •

February 5, 2009 at 13:43:54
You didn't say what's not working, but reduced to essentials:

No need for /c:yn ; it's the default.

@echo off
CHOICE /t:y,30 delete all files?
goto :%errorlevel%
del "%drive%"

If at first you don't succeed, you're about average.


Report •

Related Solutions

Ask Question