Solved

Batch File: SET WMIC OUTPUT TO VARIABLE

Posted on 2010-09-04
9
5,233 Views
Last Modified: 2012-05-10
Hi there,

In this batch file, the variables don't get set.

Thanks for your help,
Rene
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1-6 delims= " %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	ECHO NOW IS: "%%A" "%%B" "%%C" "%%D" "%%E" "%%F"
	SET Day=%%A
	SET Hour=%%B
	SET Min=%%C
	SET Month=%%D
	SET Sec=%%E
	SET Year=%%F
)

ECHO Day=%day%
ECHO Hour=%Hour%
ECHO Min=%Min%
ECHO Month=%Month%
ECHO Sec=%Sec%
ECHO Year=%Year%

PAUSE

Open in new window

0
Comment
Question by:ReneGe
9 Comments
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 500 total points
ID: 33604834
Why are you trying to get date/Time data out of wmi?

Why not use either date /t or %date% and %time%?

If you are using US standard date/time format then:
%date:~-4% is equal to the year.
%date:~4,2% is equal to the month
and %date:~7,2% is equal to the day.

Time wise, %time:~0,2% is the hour
%time:~3,2% is the minute
and %time:~6,2% is the second.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33604840
Hi leew,

I whant to use WMIC because date format may dramatically change.

I resolved it.

Here is my working code.

Thanks for your help,
Rene
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "skip=1 tokens=1-6 delims= " %%A IN ('WMIC ^Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	ECHO NOW IS: "%%A" "%%B" "%%C" "%%D" "%%E" "%%F"
	IF %%A GTR 0 (
		SET Day=%%A
		SET Hour=%%B
		SET Min=%%C
		SET Month=%%D
		SET Sec=%%E
		SET Year=%%F
		)
)

ECHO Day=%day%
ECHO Hour=%Hour%
ECHO Min=%Min%
ECHO Month=%Month%
ECHO Sec=%Sec%
ECHO Year=%Year%

PAUSE

Open in new window

0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 33604845
Then use date /t which shouldn't change.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Author Comment

by:ReneGe
ID: 33604863
Just tried "date /t" and for did change depending of Environment date format setting
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33604869
Just tried "date /t" and it's format did change depending of the Environment date format setting
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33605088
I know this is closed, I Was working today, what can I say.

Here's how I do it with WMIC when I want to go that route.  Nearly the same but wanted to share my approach when I need leading zeros pn the day and month fields since WMIC doesn't do that.

In addition, I also often use this very useful utility to get todays date into variables, or a date offset from today by a certain amount.

http://www.jfitz.com/dos/index.html#DOFF

~bp
REM First get the pieces of todays date in various formats needed
for /f "skip=2 tokens=2-4 delims=," %%A in ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:csv') do (
  REM values without leading zeros
  set "d=%%A"
  set "m=%%B"
  set "yyyy=%%C"
)
 
REM Build corresponding values with leading zeros
set "mm=%m%"
set "dd=%d%"
if %m% LSS 10 set "mm=0%m%"
if %d% LSS 10 set "dd=0%d%"

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33605132
Thanks bp

You'r a champ!

Cheers,
Rene
0
 

Expert Comment

by:huhuman
ID: 35983682
Hey reneGe ,

May I ask you something, I am not very sure why does is it that it is able to set WMIC output to variable after you have add the if  %%A is greater than 0 condition.


Thanks,
JH
0
 
LVL 10

Author Comment

by:ReneGe
ID: 35983874
This is to exclude non numeric values.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

773 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