Solved

Batch File: SET WMIC OUTPUT TO VARIABLE

Posted on 2010-09-04
9
5,168 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
 
LVL 10

Author Comment

by:ReneGe
ID: 33604863
Just tried "date /t" and for did change depending of Environment date format setting
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 51

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
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…

705 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

17 Experts available now in Live!

Get 1:1 Help Now