The NT 4.0 Approach to Time Stamping

NT contains timestamping tools, but you'll need to filter the data they retrieve.

Dick Lewis

September 7, 2003

2 Min Read
ITPro Today logo in a gray background | ITPro Today

Retrieving date and time information in a Windows NT 4.0 environment is more difficult than in a Windows Server 2003, Windows XP, or Windows 2000 environment because NT doesn't include a built-in environment variable to maintain date or time information. However, if your organization still includes NT 4.0 systems, you do have some options.

The Date and Time commands, which you can use to set the system date and time, can make use of the /t switch (which isn't case sensitive) to capture the current date and time. For example, the command

Date /t

returns the output

Fri 05/09/2003

and the command

time /t

returns the output

3:35p

Note that the Time /t command returns a "p" or an "a" rather than "pm" or "am."

The Microsoft Windows NT 4.0 Resource Kit (and the Microsoft Windows 2000 Server Resource Kit) includes the Now (now.exe) utility, which combines date and time information and includes seconds in the output time data. The tool also lets you include a message in the output; simply run now.exe with the message as an argument, and the command will echo that text in the output. For example, the command

now.exe Disk utilization metrics on Server1

returns the output

Fri May 09 15:49:40 2003 -- Disk utilizationmetrics on Server1

Both the Win2K version and the NT version of now.exe include an idiosyncrasy: The command echoes out a blank line just before the output line. To avoid that extra line, you can use a For command:

for /f "tokens=*" %%i in ('now') do echo %%i

To add a simple timestamp to a log file, you can use the > or >> symbols to redirect the output of any of these commands (i.e., Date, Time, or Now). For example, the command

Date /t>>C:logfileslogfile.txt

returns the output

Fri 05/09/2003

You might want to manipulate the result set by filtering out information such as the day-of-the-week abbreviation (e.g., "Fri"); removing the forward slashes (/), which can cause filename problems; substituting a different character for any spaces or slashes; combining the output of the Date and Time commands; and rearranging the order of the captured date and time into a format that's appropriate for your output needs. The following Date command removes the abbreviated day-of-the-week information and substitutes dashes for spaces and forward slashes.

for /f "tokens=1,2,3,4 delims=/ " %%i in ('date  /t') do echo %%i-%%j-%%k-%%l

The following example combines the Date and Time commands to give you NT 4.0 output that's similar to the Now command's output. The commands

for /f "tokens=1,2,3,4 delims=/ " %%i in ('date  /t') do Set TheDate=%%i-%%j-%%k-%%lfor /f "tokens=1,2 delims=: " %%i in ('time  /t') do echo %TheDate%-%%i-%%jm

return the output

Fri-05-09-2003-04-47pm

The following Now command moves the year data next to the date and time elements, which indicate hours (h), minutes (m), and seconds (s). This command also filters out colons (:) and forward slashes, replacing both characters with a dash (-). The command

for /f "tokens=1,2,3,4,5,6,7 delims=/: " %%i in('now /t') do echo %%i-%%j-%%k-%%o-%%lh-%%mm-%%ns

returns the output

Fri-May-09-2003-16h-40m-41s
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like