

InterviewSolution
Saved Bookmarks
1. |
Solve : Conditional sentence in MS-DOS? |
Answer» <html><body><p>Hi, I'm not an expert in MS-DOS, that's the reason of my question. How could I <a href="https://interviewquestions.tuteehub.com/tag/put-11868" style="font-weight:bold;" target="_blank" title="Click to know more about PUT">PUT</a> a conditional question in a DOS batch?<br/><br/>For example:<br/><br/><strong>Would you like to execute the next process? Y/N</strong><br/><br/>Best regards.several options; the "easiest" is to use pause:<br/><br/> Code: <a>[Select]</a>ECHO OFF<br/>pause To process the next item, press any key. To Cancel, Press Control+Break.<br/><br/>REM etc..<br/><br/>or, use CHOICE:<br/><br/> Code: <a>[Select]</a>ECHO OFF<br/>REM CLEAR DONEXT:<br/>set DONEXT=<br/>:RECHOICE<br/>choice Would you like to execute the next process?<br/>IF ERRORLEVEL==2 GOTO PRESSEDN<br/>IF ERRORLEVEL==1 GOTO PRESSEDY<br/>ECHO Please Enter Y OR N.<br/>GOTO RECHOICE<br/>:PRESSEDN<br/>REM N was pressed in CHOICE.<br/>SET DONEXT=N<br/>GOTO ENDING<br/>:PRESSEDY<br/>REM Y was pressed<br/>SET DONEXT=Y<br/>:ENDING<br/><br/>REM return to caller.<br/><br/><br/>I wrote the second one with the intent that it may be used as "module" <a href="https://interviewquestions.tuteehub.com/tag/sort-238033" style="font-weight:bold;" target="_blank" title="Click to know more about SORT">SORT</a> 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.<br/><br/>Then, in the main batch, you would simply do:<br/><br/> Code: <a>[Select]</a>REM check if we should execute the next process...<br/>DOCHOICE<br/>if %DONEXT%==Y GOTO GOTONEXT<br/>if %DONEXT%==N GOTO NONEXT<br/><br/>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 <a href="https://interviewquestions.tuteehub.com/tag/processes-21463" style="font-weight:bold;" target="_blank" title="Click to know more about PROCESSES">PROCESSES</a> run" or whatever, depending on your use case.<br/><br/>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 <a href="https://interviewquestions.tuteehub.com/tag/page-25452" style="font-weight:bold;" target="_blank" title="Click to know more about PAGE">PAGE</a> 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.<br/><br/>I'll <a href="https://interviewquestions.tuteehub.com/tag/shut-643388" style="font-weight:bold;" target="_blank" title="Click to know more about SHUT">SHUT</a> up now.Thanks a Lot!</p></body></html> | |