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

By Spike
Dec 7, 2004
  1. 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.

    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.
  2. RealBlackStuff

    RealBlackStuff TS Rookie Posts: 6,503

    This is the text for e.g. DEFSHUT.BAT:

    (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
  3. Spike

    Spike TS Evangelist Topic Starter Posts: 2,168


    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?! :)

    Attached Files:

  4. RealBlackStuff

    RealBlackStuff TS Rookie Posts: 6,503

Topic Status:
Not open for further replies.

Similar Topics

Add New Comment

You need to be a member to leave a comment. Join thousands of tech enthusiasts and participate.
TechSpot Account You may also...