Avatar of ReneGe
ReneGe
Flag for Canada asked on

Batch File: SET WMIC OUTPUT TO VARIABLE

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

Windows BatchProgrammingMicrosoft DOS

Avatar of undefined
Last Comment
ReneGe

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Lee W, MVP

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ReneGe

ASKER
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

Lee W, MVP

Then use date /t which shouldn't change.
ReneGe

ASKER
Just tried "date /t" and for did change depending of Environment date format setting
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ReneGe

ASKER
Just tried "date /t" and it's format did change depending of the Environment date format setting
Bill Prew

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

ReneGe

ASKER
Thanks bp

You'r a champ!

Cheers,
Rene
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
huhuman

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
ReneGe

ASKER
This is to exclude non numeric values.