Solved How do I turn a batch commands output into a variable?

September 7, 2015 at 13:24:50
Specs: Windows 7, 6
What I am trying to do is create a program that works like a schedule. What the program would check the day using "wmic path win32_localtime get dayofweek" and turn that output into a variable.

If you don't know how the commands output works it gets the day of the week and turns it into a numeric form.

Example: If the day was Monday the command would output
DayOfWeek
1
Tuesday would output
DayOfWeek
2
and so on


See More: How do I turn a batch commands output into a variable?

Report •


#1
September 7, 2015 at 17:26:03
✔ Best Answer
Try
@Echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims==" %%A in ('wmic path win32_localtime get dayofweek /format:value') do (
	set day=%%A
	echo **!day!**>x
	for /f %%B in (x) do (
		set day=%%B
	)
        del /q x > nul
	set day=!day:~2,-3!
)

The reason !day! is echoed into a file, then read again is because you can't perform operations with the number without removing some "invisible" characters from the string.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •

#2
September 7, 2015 at 21:32:10
Rainbawz good catch on the unicode! This seemed to work as well, (perhaps):
@for /f "tokens=2 delims==" %%a in ('wmic path win32_localtime get dayofweek /format:value') do set dofw=%%a
@set %dofw%
::====

Report •

#3
September 9, 2015 at 15:46:45
Sorry my response has taken so long I have been a bit delayed due to personal issues, RainBawz and nbranes code work perfectly fine and thank you for that but when I tried nbranes code I found that it puts it into a variable just fine but it also displays the error code

"Environment variable 3 not defined"


Report •

Related Solutions

#4
September 9, 2015 at 20:18:51
My bad. last line should have been:
set dofw
not:
set %dofw%
---- all that does is show the var. value, same as: echo %dofw%
Anyway, Rbawz got the answer, so it's all good!

Report •


Ask Question