Using the environment variables DATE
and TIME
is very fast. But it has the disadvantage that the format of date and time string depends on Windows Region and Language settings for the current user. So what works on one computer for one user must not work for another user on same or a different computer.
For example the command line
@cls & @echo Date: %DATE% & @echo Time: %TIME%
executed in a command prompt window outputs on my computer with my region settings:
Date: 19.08.2016
Time: 12:58:33,01
But there is a command to get the current local date time always in a region and language independent format:
wmic OS get LocalDateTime
The command wmic - Windows Management Instrumentation command-line utility - with these options outputs to console on my computer the two lines (plus a third blank line):
LocalDateTime
20160819125833.203000+120
The date/format is fixed with YYYYMMDDHHmmss
plus a .
plus microsecond
with always 6 digits plus current ± time offset to UTC
with always 3 digits.
The region independent date/time string can be easily reformatted to any other date/time format like YYYY-MM-DD_HH_mm_ss
as shown in code below:
@echo off
for /F "skip=1 delims=." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime') do set "LocalDateTime=%%T" & goto ReformatDateTime
:ReformatDateTime
set "CurrentTime=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%_%LocalDateTime:~8,2%_%LocalDateTime:~10,2%_%LocalDateTime:~12,2%"
set "JAVA_HOME=C:\jdk1.5.0_05"
set "CLI_HOME=c:\projects\utds\applications\cli"
set "CLI_LIB=%CLI_HOME%\lib"
set "CLASSPATH=%CLI_LIB%\commons-logging.jar;%CLI_LIB%\commons-logging-api.jar"
set "CLASSPATH=%CLASSPATH%;%CLI_LIB%\spring.jar;%CLI_LIB%\spring-core.jar;%CLI_LIB%\spring-support.jar;%CLI_LIB%\spring-remoting.jar"
set "CLASSPATH=%CLASSPATH%;%CLI_LIB%\utds-infra.jar;%CLI_HOME%\src\conf\spring;%CLI_HOME%\src\conf"
set "CLASSPATH=%CLASSPATH%;%CLI_LIB%\aopalliance.jar"
set "CLASSPATH=%CLASSPATH%;%CLI_HOME%\dist\cli.jar;%JAVA_HOME%\jre\lib\ext\comm.jar"
set "PATH=%JAVA_HOME%\bin;%PATH%"
rem > will simply overwrite the log file
>"mylogs.txt" echo (%CurrentTime%) java -Dport=COM3 -DbaudRate=9600 -Dparser=lgCliParser -DappContext=applicationContext-service.xml com.utds.cli.service.comm.CallerIdListener
pause
Well, for the third line I suggest to use the following line which creates the current date/time string in format YYYY-MM-DD HH:mm:ss
which is better readable, but can't be used for file/folder names because of the colons in time string.
set "CurrentTime=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2% %LocalDateTime:~8,2%:%LocalDateTime:~10,2%:%LocalDateTime:~12,2%"
Another solution using wmic
is running this command with:
wmic OS get LocalDateTime /VALUE
The option /VALUE
results in a different format for the output:
LocalDateTime=20160819125833.203000+120
The batch file adapted for this output:
@echo off
for /F "tokens=2 delims==." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime /VALUE') do set "LocalDateTime=%%T"
set "CurrentTime=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%_%LocalDateTime:~8,2%_%LocalDateTime:~10,2%_%LocalDateTime:~12,2%"
set "JAVA_HOME=C:\jdk1.5.0_05"
set "CLI_HOME=c:\projects\utds\applications\cli"
set "CLI_LIB=%CLI_HOME%\lib"
set "CLASSPATH=%CLI_LIB%\commons-logging.jar;%CLI_LIB%\commons-logging-api.jar"
set "CLASSPATH=%CLASSPATH%;%CLI_LIB%\spring.jar;%CLI_LIB%\spring-core.jar;%CLI_LIB%\spring-support.jar;%CLI_LIB%\spring-remoting.jar"
set "CLASSPATH=%CLASSPATH%;%CLI_LIB%\utds-infra.jar;%CLI_HOME%\src\conf\spring;%CLI_HOME%\src\conf"
set "CLASSPATH=%CLASSPATH%;%CLI_LIB%\aopalliance.jar"
set "CLASSPATH=%CLASSPATH%;%CLI_HOME%\dist\cli.jar;%JAVA_HOME%\jre\lib\ext\comm.jar"
set "PATH=%JAVA_HOME%\bin;%PATH%"
rem > will simply overwrite the log file
>"mylogs.txt" echo (%CurrentTime%) java -Dport=COM3 -DbaudRate=9600 -Dparser=lgCliParser -DappContext=applicationContext-service.xml com.utds.cli.service.comm.CallerIdListener
pause
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
echo /?
for /?
pause /?
rem /?
set /?
wmic /?
wmic os /?
wmic os get /?
Note: Avoid a space between text and redirection operation >
on redirecting a message output with echo
to a text file as this space is also written as trailing space into the file. A simple method avoiding this trailing space working in most cases is first specifying the redirection operator and the file name and second the command echo
with the text to write to the file with no trailing space.