[SOLVED] Setting A Variable To The Output of A Command?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nitt
Posts: 218
Joined: 22 Apr 2011 02:43

[SOLVED] Setting A Variable To The Output of A Command?

#1 Post by nitt » 30 May 2011 12:58

I tried like

Code: Select all

echo hi|set /p var=


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.
Last edited by nitt on 30 May 2011 13:48, edited 1 time in total.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Setting A Variable To The Output of A Command?

#2 Post by Cleptography » 30 May 2011 13:12

If you want the shortest easiest answer then my answer is google it, it is there.
http://www.tomshardware.com/forum/230090-45-windows-batch-file-output-program-variable

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Setting A Variable To The Output of A Command?

#3 Post by nitt » 30 May 2011 13:16

Cleptography wrote:If you want the shortest easiest answer then my answer is google it, it is there.
http://www.tomshardware.com/forum/230090-45-windows-batch-file-output-program-variable


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.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Setting A Variable To The Output of A Command?

#4 Post by nitt » 30 May 2011 13:43

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/
)

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: [SOLVED] Setting A Variable To The Output of A Command?

#5 Post by orange_batch » 30 May 2011 15:47

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.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: [SOLVED] Setting A Variable To The Output of A Command?

#6 Post by nitt » 30 May 2011 15:59

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: [SOLVED] Setting A Variable To The Output of A Command?

#7 Post by dbenham » 30 May 2011 17:00

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 string

Dave Benham

Post Reply