Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

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

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

SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

Thanks mates :)
Greatly appreciated!
Cheers
Avatar of ReneGe

ASKER

@ Qlemo
I'm pinging a subnet.  Not an actual IP address ;-)
Cheers mate!
That returns immediately, too.
Avatar of ReneGe

ASKER

Strange, not me.  I get something close to a second.  Cheers
Avatar of ReneGe

ASKER

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)
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.