9/14/2015 - 12:11 PM

[2014] sleep.bat

Windows script to sleep for a while.
Compatible with WinXP, Win7, and newer.
MUST be called with a "call sleep.bat xx" line because the "exit" statement would close the caller script otherwise.

REM Windows sleep script by Justin MASSIOT
REM Example use to sleep for 15 seconds: "call sleep.bat 15"

@echo off

REM Check if the sleep time is defined
if "%1"=="" ( goto usage )
REM Check if the time is greater than 0
if %1 leq 0 ( goto usage )
REM When using the "ping" hack, we must add 1 sec to the time because there is an offset
set /a time_ping=%1+1

REM Extract the numeric version of Windows from "ver.exe": "... [version X.X ...]"
for /f "tokens=2 delims=[" %%a in ('ver ^| findstr /v "linux"') do @set winver=%%a

REM Then we get the X.X version by removing the 8 first characters
REM Extract the version number (X.X), and do different actions if greater or equal than 6.0 (Vista and newer) or less than 6.0 (XP and older)
if %winver:~8,3% geq 6.0 (
  REM Windows Vista, 7 and newer
  timeout %1
) else (
  REM Windows XP, 2000 (and older?)
  REM The following line is a HACK to sleep in Win XP and older
  echo Waiting for %1 second^(s^)...
  ping -n %time_ping% > nul
goto eof

echo Usage:
echo SLEEP time_in_seconds
sleep 2