none
Working on a Hibernate timer .bat script RRS feed

  • Question

  • I am trying to write a simple script that will put my Win.7 computer into 'Hibernate' after a designated number of minutes. I want this to open from my desktop, prompt me to input how many minutes I would like to wait, and then execute the operation when the time comes. I have other ideas on how to improve the script, but I would really just like basic function operable. I am very new to writing scripts and started with someone else's script, which looks like this:

    HibTimer.bat

    @echo off
    set /p time=How many minutes until hibernate? :
    cls
    for /L %%A in (%time%,-1,0) do (
    echo shutdown -h in %%A minutes
    ping localhost -n 60 >null
    cls
    )
    shutdown -h

    As is, this has worked decently. The two big issues I have with it are:

    1) Inputting high values upon the prompt causes the hibernation to occur immediately.

    2) The script creates a blank desktop icon titled 'null'

    Can someone please help me understand what this script is doing (particularly after the "for /L" bit?) And how I could maybe improve it to better deal with my issues?

    Honestly, I would have preferred to have started from scratch, but I am trying to get something working sooner than later to resolve a (completely different) reoccuring issue I am experiencing with my built-in Windows inactivity hibernate schedule.

    Thanks 1000x


    • Edited by StarkTony Wednesday, April 9, 2014 8:06 AM
    Wednesday, April 9, 2014 8:05 AM

Answers

  • %time% is an inbuilt variable. You should not use it for your own purposes.
    Your code directs the output from ping.exe to a file called "null". To suppress the output you must use the "nul" device (single L).

    This code should work:

    @echo off
    set /p timeout=How many minutes until hibernate? 
    set /a timeout=%timeout% * 60
    timeout /t %timeout% > nul
    echo shutdown -h



    Wednesday, April 9, 2014 8:26 AM

All replies

  • %time% is an inbuilt variable. You should not use it for your own purposes.
    Your code directs the output from ping.exe to a file called "null". To suppress the output you must use the "nul" device (single L).

    This code should work:

    @echo off
    set /p timeout=How many minutes until hibernate? 
    set /a timeout=%timeout% * 60
    timeout /t %timeout% > nul
    echo shutdown -h



    Wednesday, April 9, 2014 8:26 AM
  • Now that is a pretty code! (something I can actually follow ^_^)

    I fine-tuned it a bit, and included the actual command at the end and it seems to be working much more smoothly than my original.


    HibTimer.bat

    @echo off
    set /p timeout=How many minutes until hibernate?
    echo Computer will hibernate in %timeout% minutes
    set /a timeout=%timeout%*60
    timeout /t %timeout% > nul
    shutdown -h

    A couple things to keep in mind with this script, I've found that pressing any key beyond the initial prompt will trigger the hibernation immediately (I wonder if there's a way to minimize the window to prevent this from happening accidentally?), and the only way to abort the process is by closing out the window. I'm not sure if this would work or not, but perhaps bringing up an ordinary command prompt would allow me to command an abort if I wanted, and also help to prevent accidental immediate hibernation?

    Finally, I wanted to comment on something I noticed the original script would do that I don't believe this one does, and that is the read-out now doesn't tick down. [ie. before if I input 10 minutes, after 2 minutes passed, it would display that the event would occur in 8 minutes.] Not really that important in terms of functionality, but definitely a neat trick.

    Anywho, thank you so much for you assistance Forest brook! I'll be sure to update this thread if I make any further changes, and will definitely check back to see if you or others have any more input.

    :) Happy Programming! (:



    • Edited by StarkTony Wednesday, April 9, 2014 10:54 AM
    Wednesday, April 9, 2014 10:51 AM
  • Open a Command Prompt with cmd.exe, then type timeout /? to see the various options. This is the standard method to get help for the vast majority of commands. You should find the answers to all your questions. You should also play with the command without redirecting its output so that you can really see what it does and how it works.
    Wednesday, April 9, 2014 11:16 AM