Demartek Lab Tips — Time-related Scripting Functions
We do a fair amount of scripting in our validation and performance testing, and we often require time-related functions for our batch files on our Windows machines. These time-related functions can be divided into three categories:
- Display the date and/or time
- Delay execution (wait) for a specified period of time
- Synchronize the clocks between machines
Display the Date and/or Time
The get the date and/or time in a batch file, simply use the native date or time commands with the “/t” option. The “/t” option simply displays the date or time without changing it. These two commands work well if you need to capture a simple date or time. These two commands have been available in Windows for many years.
A handy way to get a little more precision with the date or time commands is to pipe them with the echo command like this:
echo | time | find "is:"Which results in:
The current time is: 14:16:11.37
However, we like to better document our scripts to provide additional information with a time stamp. For this, I’ve been using the now.exe utility that has been available for several years, originally appearing in the Windows 2000 Resource Kit from Microsoft and is also available in the Windows Server 2003 Resource Kit (see links below). The now.exe utility not only provides the date and time together on one output line, but also allows for optional text to be displayed with it. For example, we often use something like the following command in our scripts:
now step 1 of 4The output from this command is
Mon Jul 25 09:07:01 2011 -- step 1 of 4This output provides not only the date and time but some documentation regarding the progress in the batch file. We like to pipe this output into a job log text file.
It is often necessary to wait for a period of time to allow disk caches to completely flush or other threads to finish before proceeding with subsequent steps in a batch file. Recent versions Windows have added the timeout command that can be used for this purpose. The simple form of the timeout command uses the “/t” option to indicate a number of seconds to wait. The command simply waits, has no output, and in its most simple form, is used like this:
timeout /t 10This tells the script to wait 10 seconds before proceeding. There are a couple of other options available to ignore key presses or wait indefinitely.
If you are using Windows XP or older, there is no native timeout command. For many years, I have been using sleep.exe, which was originally provided in the Windows 2000 Resource Kit and is also available in the Windows Server 2003 Resource Kit (see links below). Sleep.exe is a very simple utility that waits the desired number of seconds, and is used like this:
sleep 10This tells the script to wait 10 seconds before proceeding.
If you are not running unattended and want something a little more interactive, you might want to include the choice command in a loop that can wait for some time while prompting for an answer. Choice.exe is available on recent versions of Windows. The following code snippet shows how this works:
Echo Do something here
choice /t 10 /m "Waiting for 10 seconds. Continue? " /c ny /d y
if errorlevel 2 goto loop
Synchronizing the clocks on multiple machines is essential when running tests, especially when using virtual machines. The net time command has been available in Windows for quite a while and is used to synchronize the clock on the current machine to the clock on another machine. The net time command must be used on an account with full Administrator privileges, or using the “run as Administrator” option. In this example, the machine with the correct time is called PrimaryMachine. The syntax for the net time command in a script is:
net time \\PrimaryMachine /set /yThe /y option is used to automatically answer the “do you really want to do this” question.
- Windows 2000 Resource Kit: http://support.microsoft.com/kb/927229
- Windows Server 2003 Resource Kit: http://www.microsoft.com/download/en/details.aspx?id=17657
To be notified when we post new deployment guides, evaluation reports and commentaries, sign-up for our free monthly newsletter, Demartek Lab Notes. We do not give out, rent or sell our email list.