Solved

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

Posted on 2016-11-05
8
127 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 70

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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 10

Author Comment

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

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 70

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …

751 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