Enterprise Software

Retrieve the date and time in Windows 2000 Professional batch files

If you're sorting batch files according to date and time, Windows 2000 Professional columnist Jim Boyce shows how to retrieve the information and export it to a handy log or text file.

Sometimes it's useful to retrieve the current date and time from within a batch for a command file. For instance, say you create a batch file to perform a backup or copy operation and want the program to write the current date and time for specific actions to a log file. Or assume you create a batch file that (among other tasks) copies several folders from one location to another. Now you'll want to export the date and time the action is completed to a log file.

You have a couple of options for extracting the current date and time. One is to use the ECHO command in the batch file. You can use the %date% and %time% variables to extract the date and time, and use the > and >> redirection symbols to redirect the text to a file. The > symbol creates a file and >> appends to a file. For example, use the following command to append the date, time, and a message to a text file:

echo %date% %time% File copy operation complete. >> log.txt

Another option is to use the Now.exe tool included with the Windows 2000 Resource Kit. Now.exe extracts the current date and time and displays it on the standard output (by default, the screen). You can use redirection to direct the text to a file. The following is an example that appends the current date, time, and a message to a text file:

Now File copy operation complete. >> log.txt

To use Now.exe in a batch file, install the Windows 2000 Resource Kit and make sure the path variable points to the folder containing Now.exe (or place it in the folder from which you're executing the batch file).

Miss a column?

Check out the Windows 2000 Professional archive, and catch up on Jim Boyce's column.

Want more Win2K tips and tricks? Automatically sign up for our free Windows 2000 Professional newsletter, delivered each Tuesday!

 

0 comments

Editor's Picks