Page 1 of 1

Hostname in batch file

Posted: 16 Oct 2006 11:31
by notb
In a batch file, how can I get the return value of a command into a variable? For example, I'd like to have a variable set with the 'hostname' return value.

Posted: 17 Oct 2006 18:42
by DosItHelp
notb,

You can read the output of a command into a variable using the FOR command:

Code: Select all

for /f "delims=" %%a in ('command') do set varible=%%a


See also: http://www.dostips.com/DosCommandRef.htm#FOR

If 'command' returns multiple lines then variable will capture the last line of output. With a little more effort any line can be captured. Let me know what you need.

Note that the hostname of your PC is already in a variable called %computername% and you might not need the FOR command.

DOS IT HELP? :wink:

Posted: 18 Oct 2006 13:45
by notb
Thanks for the tip on the FOR loop, I didn't know.

As far as the presence of the env var is concerned, wow, that's embarrassing :oops:

I'm certainly all set now.