Solved

Windows Batch File - Display current time and date in FOR command

Posted on 2016-11-05
8
76 Views
Last Modified: 2016-11-07
Hi there,

This code will display the date and time 5 times.

The problem is that although it reads the current time at every cycle, it still display the first time read.

I need it to display the current date time at every cycle.

I know it can be done cause I've done it in the fast.  But I don't remember how.

Thanks for your help,
Rene

@ECHO OFF
CLS

FOR /L %%a in (1,1,5) DO (
	CALL :GetDateTime
	ECHO %TheDate% %TheTime%
	PING 10.0.0.0 -n 1 -w 1000 >NUL 2>&1
)

EXIT /b

:GetDateTime
FOR /F "tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Year^,Month^,Day^,Hour^,Minute^,Second ^| FINDSTR /R [0123456789]') do (
	IF %%A GEQ 10 (set dd=%%A) ELSE (set dd=0%%A)
	IF %%B GEQ 10 (set hh=%%B) ELSE (set hh=0%%B)
	IF %%C GEQ 10 (set min=%%C) ELSE (set min=0%%C)
	IF %%D GEQ 10 (set mm=%%D) ELSE (set mm=0%%D)
	IF %%E GEQ 10 (set ss=%%E) ELSE (set ss=0%%E)
	IF %%F GEQ 10 (set yy=%%F) ELSE (set yy=0%%F)
)

SET TheDate=%yy%-%mm%-%dd%
SET TheTime=%hh%.%min%.%ss%

EXIT /b

Open in new window

0
Comment
Question by:ReneGe
  • 4
  • 3
8 Comments
 
LVL 24

Assisted Solution

by:NVIT
NVIT earned 250 total points
ID: 41875706
Add as Line 2...
SETLOCAL ENABLEDELAYEDEXPANSION

Open in new window


Change Line 6 to...
ECHO !TheDate! !TheTime!

Open in new window

0
 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 41875714
First, it is a complicated way to get the date, though some people agree on using it. As long as you are not in a very volatile environment with changing date formats, it is much easier to just use the respective parts of %date% and %time%.

Leaving that asside, you need to use delayed expansion inside of parens if the vars get changed inside.
@ECHO OFF
CLS
setlocal EnableDelayedExpansion

FOR /L %%a in (1,1,5) DO (
	CALL :GetDateTime
	ECHO !TheDate! !TheTime!
	PING 10.0.0.0 -n 1 -w 1000 >NUL 2>&1
)

EXIT /b

Open in new window

That ping doesn't perform any delay, btw. A single ping to a reachable destination is instantaneous.
0
 
LVL 10

Author Closing Comment

by:ReneGe
ID: 41875814
Thanks mates :)
Greatly appreciated!
Cheers
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Author Comment

by:ReneGe
ID: 41875817
@ Qlemo
I'm pinging a subnet.  Not an actual IP address ;-)
Cheers mate!
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41875950
That returns immediately, too.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41876322
Strange, not me.  I get something close to a second.  Cheers
0
 
LVL 10

Author Comment

by:ReneGe
ID: 41876324
C:\WINDOWS\system32>ping 10.0.0.0

Pinging 10.0.0.0 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 10.0.0.0:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41876966
Oops, you are correct. I confused broadcast and network address. If you ping the broadcast address (10.255.255.255 or whatever applies for your network), everybody answers, and ping -n 1 does not wait.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question