-2

I have following batch file Java code, I want add date time also in mylogs.txt anyone help me, I don't know how to add datetime with seconds in Java batch.

 @echo off

    set Year=%DATE:~10,4%
    set Month=%DATE:~4,2%
    set Day=%DATE:~7,2%

    set Hour=%TIME:~0,2%
    set Minute=%TIME:~3,2%
    set Second=%TIME:~6,2%

    rem Modify this line to fit your time format
    set CurrentTime=%Year%-%Month%-%Day%_%Hour%_%Minute%_%Second%

    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
    echo (%CurrentTime%) java -Dport=COM3 -DbaudRate=9600 -Dparser=lgCliParser -DappContext=applicationContext-service.xml com.utds.cli.service.comm.CallerIdListener > mylogs.txt

    pause

When I add echo (%CurrentTime%) its save below data in text file (it's wrong)

enter image description here enter image description here

 java -Dport=COM3 -DbaudRate=9600 -Dparser=lgCliParser -DappContext=applicationContext-service.xml` com.utds.cli.service.comm.CallerIdListener > mylogs.txt

When I remove echo (%CurrentTime%) its working fine command window still working fine and when I call command window show got event and save below result in text file.

 got message '001 : 1234567885' got message '001 : 1234567885 ->  101' {phoneNo=1234567885, handsetId=101}

I want also datetime with seconds. How do I this?

enter image description hereenter image description here

Addi khan
  • 11
  • 6
  • Change the statement as `set CurrentTime=%Year%_%Month%_%Day%_%Hour%_%Minute%_%Second%` – Sujeet Sinha Aug 18 '16 at 12:23
  • i want caller details with datetime seconds – Addi khan Aug 18 '16 at 12:31
  • check this link [link](http://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script) – Karthik Aug 18 '16 at 12:31
  • i was check this link but i have java code in batch file thats why its not working for me – Addi khan Aug 18 '16 at 12:35
  • Echo will just print what ever available in the next to that .You should check for echo command without a new line here [Windows echo wihout new line link](http://stackoverflow.com/questions/7105433/windows-batch-echo-without-new-line) and you have to execute java command in next line . I think in windows 7 on wards we can combine two commands [how to run two commands in same line](http://stackoverflow.com/questions/8055371/how-to-run-two-commands-in-one-line-in-windows-cmd) – Karthik Aug 18 '16 at 13:28
  • You are not having java code in batch file. You are just calling java.exe from command prompt. – Karthik Aug 18 '16 at 13:35
  • @Addikhan Why do you post text stored in a text file as screen capture? Why do you not copy & and paste the text __as text__ into your question so that it can be seen immediately and also in 5 years? Right clicking into a command prompt window opens the context menu with context menu item __Mark__. After clicking on this context menu item the text in the console window can be marked with the mouse and copied __as text__ to clipboard by hitting key RETURN and pasted into the edit field in the browser as text. Please update your question with the text instead of the screen captures. Thanks. – Mofi Aug 19 '16 at 10:29

1 Answers1

0

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143