Solved

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

Posted on 2016-11-05
8
49 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 23

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 68

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
 
LVL 10

Author Comment

by:ReneGe
ID: 41875817
@ Qlemo
I'm pinging a subnet.  Not an actual IP address ;-)
Cheers mate!
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 68

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 68

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now