Solved

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

Posted on 2016-11-05
8
111 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 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 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