Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch File: SET WMIC OUTPUT TO VARIABLE

Posted on 2010-09-04
9
Medium Priority
?
5,605 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 96

Accepted Solution

by:
Lee W, MVP earned 2000 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 96

Expert Comment

by:Lee W, MVP
ID: 33604845
Then use date /t which shouldn't change.
0
Industry Leaders: 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!

 
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 59

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Six Sigma Control Plans
Loops Section Overview

926 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