views:

2018

answers:

7

Windows's Snipping tool can capture the screen, but sometimes I want to capture the screen after 5 seconds, such as taking an image being displayed by the webcam. (run the script and smile at the camera, for example).

So in Ruby, I could do something like

sleep 3
system('c:/windows/system32/SnippingTool.exe')

but not all computer has Ruby, so how do I do that in a .bat file? (something that is runnable on most PC with Snipping tool).

The problem is that there is no "sleep" usable in a .bat file.

+3  A: 

You can use vbs: myscript.vbs:

set wsobject = wscript.createobject("wscript.shell")

do while 1=1
  wsobject.run "SnippingTool.exe",0,TRUE
  wscript.sleep 3000
loop

batch file:

cscript myscript.vbs %1
Dani
+7  A: 

One hack I have seen is to use the ping command to attempt to ping an invalid ip:

ping 1.1.1.1 -n 1 -w 3000 > nul

1.1.1.1 invalid IP, can never be reached.
-n 1 only attempt to connect once.
-w 3000 wait 3 seconds for reply.
nul gobble the output.

Martin
+2  A: 
rem *** HACK ALERT: Sleep for 5 seconds ***
ping -n 5 127.0.0.1 > nul
rem ***************************************
RichAmberale
This should be `-n 6`. Otherwise you just wait 4 seconds. Remember that `ping` waits 1 second *between* pings, so you always have to specify one more try than you need.
Joey
A: 

By using "ping" the -n will determine the timeout only when there is no response to the ping. Check out this post about implementing DELAY as a batch file.

DELAY command implemented as a Batch File

I could just copy-paste the important bits, but the whole post is quite useful.

Nikos Steiakakis
+3  A: 

I'm very surprised no one has mentioned:

C:\> timeout 5
asveikau
'timeout' is not recognized as an internal or external command,operable program or batch file. (On my Windowx XP SP3)
RichAmberale
it works on Win 7, but not on Win XP
動靜能量
I'm fairly certain I've used it on Server 2003 (same code base as XP), so it's a wonder it's not on XP then...
asveikau
Because XP is not Server 2k3. Heck, it's two years older and historically (excluding NT 4) the server OSes came with some stuff that wasn't there in the workstation variant.
Joey
timeout was introduce with Vista according to this site http://commandwindows.com/vista-tips.htm#timeout
Adam Porad
+1  A: 

Try the Choice command. It's been around since MSDOS 6.0, and should do the trick.

Use the /T parameter to specify the timeout in seconds and the /D parameter to specify the default selection and ignore then selected choice.

The one thing that might be an issue is if the user types one of the choice characters before the timeout period elapses. A partial work-around is to obfuscate the situation -- use the /N argument to hide the list of valid choices and only have 1 character in the set of choices so it will be less likely that the user will type a valid choice before the timeout expires.

Below is the help text on Windows Vista. I think it is the same on XP, but look at the help text on an XP computer to verify.

C:\>CHOICE /?

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

Description:
    This tool allows users to select one item from a list
    of choices and returns the index of the selected choice.

Parameter List:
   /C    choices       Specifies the list of choices to be created.
                       Default list is "YN".

   /N                  Hides the list of choices in the prompt.
                       The message before the prompt is displayed
                       and the choices are still enabled.

   /CS                 Enables case-sensitive choices to be selected.
                       By default, the utility is case-insensitive.

   /T    timeout       The number of seconds to pause before a default
                       choice is made. Acceptable values are from 0 to
                       9999. If 0 is specified, there will be no pause
                       and the default choice is selected.

   /D    choice        Specifies the default choice after nnnn seconds.
                       Character must be in the set of choices specified
                       by /C option and must also specify nnnn with /T.

   /M    text          Specifies the message to be displayed before
                       the prompt. If not specified, the utility
                       displays only a prompt.

   /?                  Displays this help message.

   NOTE:
   The ERRORLEVEL environment variable is set to the index of the
   key that was selected from the set of choices. The first choice
   listed returns a value of 1, the second a value of 2, and so on.
   If the user presses a key that is not a valid choice, the tool
   sounds a warning beep. If tool detects an error condition,
   it returns an ERRORLEVEL value of 255. If the user presses
   CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
   of 0. When you use ERRORLEVEL parameters in a batch program, list
   them in decreasing order.

Examples:
   CHOICE /?
   CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
   CHOICE /T 10 /C ync /CS /D y
   CHOICE /C ab /M "Select a for option 1 and b for option 2."
   CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
Adam Porad
it is not on XP either...
動靜能量
that's interesting. This site http://www.computerhope.com/choicehl.htm states that choice is available on Windows 95, Windows 98, Windows Vista and Windows 7, but not Windows XP. I bet that MS got a lot of complaints about taking it of XP, so put they put it back into Vista.
Adam Porad
A: 

If you have an appropriate version of Windows and the Windows Server 2003 Resource Kit Tools, it includes a sleep command for batch programs. More at: http://malektips.com/xp_dos_0002.html

GreenMatt