Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-11-05
8
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 25

Assisted Solution

by:NVIT
NVIT earned 1000 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 71

Accepted Solution

by:
Qlemo earned 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Author Comment

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

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 71

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
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…

715 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