Looking for user input (Y/N) in batch script, with a timeout.

Status
Not open for further replies.

Spike

Posts: 2,122   +0
scheduling defrag for those that don't want to leave machines on overnight.

I've been looking around the web, and trawling through CMD.exe for a good few hours now, and I still can't find it.

I'm trying to create a batch file that defrags a machines c: drive, and then shuts down the computer as soon as it's finished, having given a 2 minute warning. That was the easy bit.

@echo off

defrag c:

C:\WINDOWS\SYSTEM32\TSSHUTDN.EXE 0 /DELAY:120 /POWERDOWN

I'm now struggling to find out how to get a simple (Y/N) input from a user as the script starts asking whether the user wants to defrag and shutdown or not waiting 60 seconds for an answer before procedding to defrag and shutdown the computer if no answer is given.

The idea is to schedule it to run at 2am every day, probably from Task Scheduler. It's an experiment in learning as much as anything else though, but I am now completely stuck, bogged down in pages and pages of useless information while trying to find the piece I want.

If anybody knows how to do it, I'd appreciate your help.
 
This is the text for e.g. DEFSHUT.BAT:

ECHO Want to Defrag Drive and Shutdown?
ECHO.
ECHO Press "N" if you just want to Shutdown
ECHO.
ECHO Press "Y" to Defrag and Shutdown
ECHO.
ECHO Press "Escape" to Cancel
ECHO.

C:\DOS\CHOICE /C:NY^[ /N > NUL

IF ERRORLEVEL 3 GOTO END
IF ERRORLEVEL 2 GOTO FRAG
IF ERRORLEVEL 1 GOTO SHUT

:FRAG
defrag c:

:SHUT
shutdown........

:END


(Note that `^[' represents the "Escape" character. To create it, press ^P (Control-P) in DOS Edit, then the "Escape" key. Other text editors allow one to create the "Escape" character by pressing ^V first.)
Also note the reversed questioning of the input-varaibles

To implement a WAIT command, see here: http://malektips.com/dos0017.html
 
Thankyou!!!!

For some reason, even though various people on Experts Exchange said that CMD would automatically use SET /P instead of CHOICE, youre script wouldn't work on my machine RBS. Eventually though, I managed to find the MSDOS 6.2 Supplemental download from microsoft.com, and borrowed the CHOICE.COM from it, placed it in the same folder as the batch, and it works fine.

I'm still not sure that I acctually understand what ERRORLEVEL is all about (in terms of the ways values are assigned to it by various CLI utilities, including this particular one), but that's something for me to look up. Anyway, I scheduled the task, and it runs fine. The only problem really being that if it's run as a different user than the one you are using, you don't see the shell window, and so no options appear, and also, by the time you are alerted that the computer will shutdown in 2 minutes, it's too late to stop the shutdown unless you know how, which isn't a problem really for myself, as it simply runs on the account I use anyway.

I've zipped up the working (for me anyway) XP version, and I changed the commands slightly to work on '98 and zipped that up too.

If anybody would like to tell me if the '98 one works, feel free.

I think I'll save these to CD. I think I could acctually find them quite handy lol (You know what they say about small things pleasing.... lol)

Thanks (once) again RBS! :D

Now for the biggest mystery of all. Why can't I spell properly the FIRST time I write a post?! :)
 

Attachments

  • XP dfrgshut.zip
    1.6 KB · Views: 41
  • 98 dfrgshut.zip
    1.7 KB · Views: 6
Status
Not open for further replies.
Back