Hostname in batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
notb
Posts: 2
Joined: 16 Oct 2006 11:27

Hostname in batch file

#1 Post by notb » 16 Oct 2006 11:31

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.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 17 Oct 2006 18:42

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:

notb
Posts: 2
Joined: 16 Oct 2006 11:27

#3 Post by notb » 18 Oct 2006 13:45

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.

Post Reply