Using Times and Dates in Batch Procedures
This seems to be an enduring problem, even in the Windows 95 age. Funny thing is that Microsoft hasn't seen fit to provide a simple way to access the time and date to this day, except in Win NT (see Date and Time in NT). Fortunately, the problem was solved for DOS a long time ago. This particular collection is based on other's work, but I have added my own particular spin. It is in some respects a bit of self protection since I get asked about the subject so often. Now I can just point the questioner to this page. Unlike the coverage of many other subjects here, there is an easy and simple way as well as two other methods that use slightly more complex concepts. The second approach uses a directory listing and the third uses the DOS PROMPT. Finally, there are links to useful applications of these principles.
 Method 1: Using the TIME and DATE Functions
First, the simplest (almost) example of accessing the time or the date:

:: GTD.BAT - Gets Time and/or Date and saves it into an environment variable. :: Tom Lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch/ @echo %dbgg% off if [%1]==[] %0 TIME DATE > curren}.bat echo. | %1 > current.bat echo if not [%%4]==[] shift >>current.bat echo set {%1}=%%3 call curren} if not [%2]==[] %0 %2 del curren?.bat
GTD.BAT illustrates a technique I often use to gain access to DOS utility or command information. The idea is to construct two auxilary batch files from within the running procedure. The first stores the information by redirecting the output from the utility to a file, while the second, written with one or more ECHO commands, processes the stored information. In this case, the file CURREN}.BAT contains the output from the TIME or DATE function. CURRENT.BAT transfers that information to the environment, where it is easily accessed.

The first file's name is not important, but the name of the second one must be the first word on the line output by the DOS command. In this way, running the file (CURREN}.BAT) acts to invoke the other file (CURRENT.BAT) with the desired information arrayed as command line inputs. That is, the information is made available for use in the second batch routine as the replaceable arguments %1, %2, etc. It is important to note that to make this work two percent signs in a row are required on the ECHO line used to create the second file. DOS removes one percent sign when the ECHO is first processed. For one to actually be sent to the file, two must be placed on the line.

 Method 2: Using a Directory Listing
There are a number of ways to access the date or the time in a batch file. Another approach that determines both the date and the time at its offered below. It uses the directory listing for a dummy file created when the process is run.

The date is returned in an eight-character string, MM-DD-YY for standard US format, rather than the full year, ten character, format (MM-DD-YYYY in the US). The time is also in a shortened format (HH:MMa or p). I like the short form time for display purposes, the seconds and hundredths just clutter up the display.


:: D&T.BAT - Saves Date and/or Time into a named environment variable. :: Tom Lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch/ @echo %dbgt% off %3 echo %0 %%3 %%4 :: %1 %2> {a}.bat %3 dir {a}.bat | find "{A} " > {t}.bat %3 {t} del {?}.bat if [%4]==[] echo Date: %1 Time: %2 if not [%4]==[] set %4=%1 if not [%5]==[] set %5=%2
The routine accepts one or two optional command line inputs, which are the names of variables to store the date and/or time into. If no names are provided it simply displays the results in a simple format. Therefore, to store the date into a variable called {DATE}, the syntax is ...
d&t {date}
while the date and time would require something like ...
d&t {date} {time}
and the time alone could be accessed by the trick of using {TIME} as the name for both variables on the command line, like this ...
d&t {time} {time}
 Method 3: Using the PROMPT
One of the previous examples used an ECHO. piped into the TIME function. In the US, this gives the time in a 12 hour representation. To generate a 24 hour clock format, which is sometimes more useful, the capabilities of the PROMPT command can be used instead.

The idea here is to create a dummy PROMPT using the dollar sign character equivalences to invoke the time and/or the date. Specifically, the DOS on-line HELP command (version 6 and above) offers the following explanation:

The following list shows the character combinations you can include instead of, or in addition to, any character string(s) in the text parameter. The list includes a brief description of the text or information that each character combination adds to your command prompt. $Q = (equal sign) $$ $ (dollar sign) $T Current time $D Current date $P Current drive and path $V MS-DOS version number $N Current drive $G > (greater-than sign) $L < (less-than sign) $B | (pipe) $_ ENTER-LINEFEED $E ASCII escape code (code 27) $H Backspace (to delete a character that has been written to the prompt command line)
Thus, a PROMPT string of 'Time: $T $_ Date: $D $_' would display the current time and date on separate lines. The trick is to get an appropriate output directed into a secondary batch file and then back into the calling routine. There are a number of ways, but I have generally standardized on the following technique: %comspec% /e:2048/c for %%v in (1 2) do prompt set {time}$q$t$_ | find/v "$" >{t}.bat for %%v in (call del) do %%v {t}.bat or %comspec% /e:2048/c for %%v in (1 2) do prompt set {date}$q$d$_ | find/v "$" >{t}.bat for %%v in (call del) do %%v {t}.bat The $T in a PROMPT always returns the time in 24 hour format (I think - at least it does in the US). In the example, the COMSPEC line calls a secondary command processor, which runs the FOR command, which in return changes the system prompt. All of the output of this process is passed through the FIND filter to extract the one line of interest.

It might seem that the FOR part of this command is unnecessary. However, it causes a command to be executed after the prompt is redefined. This sends a copy of the new prompt to the temporary file, {T}.BAT. Also included in the output stream is a copy of the old (existing) prompt, which is filtered out by the FIND part of the line. Fortunately, the modified prompt created by this technique does not remain because it does not become part of the master environment. It exists only as long as the secondary processor is in control.

Have I confused you? Then just ignore the explanation and copy the examples into your procedures. However, if you can grasp the concept you can use it to access system related data, such as current directory name and drive letter. Or you can use it to create elaborate ANSI display strings of keyboard macros using the $E character equivalence in place of the Escape character. A variation on this theme is explored in the ANSI.BAT procedure, described in my "Easy ANSI and PROMPT String Processing" article.

 Useful Things to Do with Time and Date information



Top | General Notes | Homepage