1.

Solve : Conditional sentence in MS-DOS?

Answer»

Hi, I'm not an expert in MS-DOS, that's the reason of my question. How could I PUT a conditional question in a DOS batch?

For example:

Would you like to execute the next process? Y/N

Best regards.several options; the "easiest" is to use pause:

Code: [Select]ECHO OFF
pause To process the next item, press any key. To Cancel, Press Control+Break.

REM etc..

or, use CHOICE:

Code: [Select]ECHO OFF
REM CLEAR DONEXT:
set DONEXT=
:RECHOICE
choice Would you like to execute the next process?
IF ERRORLEVEL==2 GOTO PRESSEDN
IF ERRORLEVEL==1 GOTO PRESSEDY
ECHO Please Enter Y OR N.
GOTO RECHOICE
:PRESSEDN
REM N was pressed in CHOICE.
SET DONEXT=N
GOTO ENDING
:PRESSEDY
REM Y was pressed
SET DONEXT=Y
:ENDING

REM return to caller.


I wrote the second one with the intent that it may be used as "module" SORT of of fashion; that is, it may be called from multiple locations in the same batch file. You can of course paste this in if there is only a single location, but otherwise, you would need to make it a new BAT file- say, "DOCHOICE.BAT" or something.

Then, in the main batch, you would simply do:

Code: [Select]REM check if we should execute the next process...
DOCHOICE
if %DONEXT%==Y GOTO GOTONEXT
if %DONEXT%==N GOTO NONEXT

You would of course define the GOTONEXT label to continue the process, and NONEXT to abort the procedure or display a warning like "not all PROCESSES run" or whatever, depending on your use case.

You'll note something particularly rare these days, is that I was able to provide a batch that actually used only DOS features, and didn't try to suggest "YOU CAN USE SET /P" or something stupid like that. That sort of annoys me when I see folks asking for help with DOS and then self-proclaimed "Batch masters" coming in with their advice that requires NT extensions. This is especially annoying since the original poster probably doesn't even know about these extensions and therefore will try to follow their advice. I've seen 3 PAGE threads here where poor folks are led on a wild-goose chase while the "batch master" tries to figure out why set /p doesn't work.

I'll SHUT up now.Thanks a Lot!



Discussion

No Comment Found