Ayush
Ayush

Reputation: 949

How to assign command value to variable in batch

I want to execute one command and assign it's value to a variable within a batch file.

We know that running hostname command on a windows command prompt gives the PC name. I want to use hostname command and assign it's value to a variable within a batch file.

After googling about it, I've tried using below methods, none of these seem to work:

set CONTROLLER=hostname
set CONTROLLER=%hostname%
set CONTROLLER=%%hostname%%
set CONTROLLER=!hostname!

Kindly advise.

Upvotes: 4

Views: 1787

Answers (2)

Rajesh Kumar Mehta
Rajesh Kumar Mehta

Reputation: 80

We can easily get the hostname/computer name through below command

set host=%COMPUTERNAME%
echo %host%

Upvotes: 4

nishantvas
nishantvas

Reputation: 304

Try Using

@echo off
for /f "delims=" %%a in ('hostname') do @set HOST=%%a
echo %HOST%
PAUSE

Where HOST is your variable and in place of 'hostname' you can use any other command you like as well.

Upvotes: 1

Related Questions