Solved

Batch File: SET WMIC OUTPUT TO VARIABLE

Posted on 2010-09-04
9
5,204 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
DevOps Toolchain Recommendations

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

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 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…

895 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

12 Experts available now in Live!

Get 1:1 Help Now