Sometimes you need to retrieve the current date and time from within a batch for a command file. For example, 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 maybe you've created a batch file that, among other tasks, copies several folders from one location to another, and you want to export the date and time the action is completed to a log file. You have a couple of options for extracting that information: the ECHO command and the Now.exe tool.
Back up important files with this simple batch file
When a user accidentally deletes a local file, having a backup ready can significantly minimize downtime and frustration. Check out this simple batch file that can make daily document backups a snap.
Extract with the ECHO command
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
The Windows Now.exe tool
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 the redirection symbols mentioned above 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.