David Metcalfe
David Metcalfe

Reputation: 2411

String concatenation with "hostname" in batch script (how to put output of "hostname" into a variable)

When I enter "hostname" into the Windows Command Prompt, I get the expected hostname of the system, but I can't seem to include it in any string output, either assigned to another variable, or just as itself.

The below script outputs "Ready to copy to Server of Is this the correct location?" and all variations of using hostname seem to result in the same.

@echo off 
set hostnamevar=%hostname%
set "greeting1=Ready to copy to Server of "
set "greeting2=Is this the correct location?"
set "greeting=%greeting1%%hostnamevar%%greeting2%"
echo %greeting%
pause 

Upvotes: 0

Views: 3121

Answers (1)

Potato Head
Potato Head

Reputation: 143

Here, hostname is set as an environment variable now.

@ECHO OFF
FOR /F %%H IN ('hostname') DO SET hostnamevar=%%H
SET Greeting=Ready to copy to Server of "%hostnamevar%" Is this the correct location?
ECHO %Greeting%
PAUSE

A little note, on this section, SET Greeting=Ready to copy to Server of "%hostnamevar%" Is this the correct location? you don't need the encapsulating "" around %hostnamevar%

Upvotes: 2

Related Questions