Solved

Batch File: SET WMIC OUTPUT TO VARIABLE

Posted on 2010-09-04
9
5,301 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
[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
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
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 54

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

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

726 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