How to assign a variable value to a variable in batch mode

I want to execute one command and assign it the value of a variable in a batch file.

We know that the host name command at the Windows command prompt gives the name of the PC. I want to use the hostname command and assign it a variable value in a batch file.

After searching it, I tried using the methods below, none of them work:

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

Please inform.

+4
source share
2 answers

We can easily get the host / computer name with the command

set host=%COMPUTERNAME%
echo %host%
+4
source

Try using

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

Where HOST is your variable, and instead of "hostname" you can use any other command that you like.

+1

Source: https://habr.com/ru/post/1672781/


All Articles