Page 1 of 1
[SOLVED] Setting A Variable To The Output of A Command?
Posted: 30 May 2011 12:58
by nitt
I tried like
And that doesn't work. The only way I know how to do it is
Code: Select all
@echo off
echo hi > tmp
set /p var=< tmp
del tmp
echo %var%
pause
%var% would be "hi".
Please also explain why your code works, and don't post some code you know is 10x's harder than it needs to be just to make yourself look smarter. So many people do this. I want the simplest way that does not require using a tmp file.
Re: Setting A Variable To The Output of A Command?
Posted: 30 May 2011 13:12
by Cleptography
Re: Setting A Variable To The Output of A Command?
Posted: 30 May 2011 13:16
by nitt
That's not an answer, that's providing the resources to figure out the answer for myself.
And I still can't get it to work after looking through that page. The only working solution is the same I posted.
Re: Setting A Variable To The Output of A Command?
Posted: 30 May 2011 13:43
by nitt
Never mind I was playing around and figured it out.
Code: Select all
@echo off
for /f %%a in ('msgbox "Do you want to go to Google.COM?" "=D" 4g') do (set dog=%%a)
if "%dog%" EQU "Yes" (
start iexplore http://www.google.com/
)
Re: [SOLVED] Setting A Variable To The Output of A Command?
Posted: 30 May 2011 15:47
by orange_batch
You're trying to pipe the output of one command into a variable.
The absolute most basic way, as you seem to have discovered:
Code: Select all
for /f "delims=" %%a in ('"command"') do set "variable=%%a"
Disabling delims makes it so the whole line stays intact. If you use "tokens=*" instead, it will remove any leading spaces/tabs. And of course, setting delims and tokens to something specific allows for more advanced processing.
Re: [SOLVED] Setting A Variable To The Output of A Command?
Posted: 30 May 2011 15:59
by nitt
orange_batch wrote:You're trying to pipe the output of one command into a variable.
The absolute most basic way, as you seem to have discovered:
Code: Select all
for /f "delims=" %%a in ('"command"') do set "variable=%%a"
Disabling delims makes it so the whole line stays intact. If you use "tokens=*" instead, it will remove any leading spaces/tabs. And of course, setting delims and tokens to something specific allows for more advanced processing.
Thanks. :3
Re: [SOLVED] Setting A Variable To The Output of A Command?
Posted: 30 May 2011 17:00
by dbenham
That's not quite the entire story with the FOR /F - By default it will ignore lines that begin with a semicolon. For a good discussion on this as well as a solution, look toward the end of this post:
Sorting tokens within a stringDave Benham