Hostname in batch file
Moderator: DosItHelp
Hostname in batch file
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.
notb,
You can read the output of a command into a variable using the FOR command:
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?
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?