56

I would like to convert this /bin/sh syntax into a widely compatible Windows batch script:

host=`hostname`
echo ${host}

How to do this so that it'll work on any Windows Vista, Windows XP, and Windows 2000 machine?

To clarify: I would then like to go on in the program and use the hostname as stored in the variable host. In other words, the larger goal of the program is not to simply echo the hostname.

lospejos
  • 1,976
  • 3
  • 19
  • 35
Edward Q. Bridges
  • 16,712
  • 8
  • 35
  • 42
  • 1
    as others have commented - just use pre-existing environment variable `%COMPUTERNAME%` instead – JGFMK Jun 18 '21 at 10:24

6 Answers6

68

hmm - something like this?

set host=%COMPUTERNAME%
echo %host%

EDIT: expanding on jitter's answer and using a technique in an answer to this question to set an environment variable with the result of running a command line app:

@echo off
hostname.exe > __t.tmp
set /p host=<__t.tmp
del __t.tmp
echo %host%

In either case, 'host' is created as an environment variable.

Community
  • 1
  • 1
sean e
  • 11,792
  • 3
  • 44
  • 56
58

I usually read command output in to variables using the FOR command as it saves having to create temporary files. For example:

FOR /F "usebackq" %i IN (`hostname`) DO SET MYVAR=%i

Note, the above statement will work on the command line but not in a batch file. To use it in batch file escape the % in the FOR statement by putting them twice:

FOR /F "usebackq" %%i IN (`hostname`) DO SET MYVAR=%%i
ECHO %MYVAR%

There's a lot more you can do with FOR. For more details just type HELP FOR at command prompt.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • 1
    very nice, though that for loop construct is heavily overloaded IMO. Will switch to use this. Thanks! – Edward Q. Bridges Jun 16 '09 at 13:05
  • 1
    You're right - FOR is basically the swiss-army knife of batch scripts in that you end up using it to solve most problems. – David Webb Jun 16 '09 at 13:13
  • If you want to repeat the assignment (For this example it does not make sense, as you would only query the host name once in a run) then don't forget to use `SETLOCAL ENABLEDELAYEDEXPANSION` (see `HELP SETLOCAL`) and use `!MYVAR!` instead of `%MYVAR%` – PA. Sep 21 '10 at 12:36
  • 11
    The 'hostname' command returns the same thing that is already stored in the %COMPUTERNAME% environment variable. – Dylan Beattie Apr 16 '11 at 13:34
  • 6
    The problem with `%COMPUTERNAME%` is that it is in upper case, and `hostname` returns the original case of the host name. If this matters, then the `hostname` approach is needed. – Adam Lindberg Jun 21 '11 at 12:48
  • >echo %hostname% %hostname% >echo %computername% mycomputername These are Windows 7 command line samples (tried both from restricted account and administrator account). As you can observe, the %computername% env variable is the only one working, the %hostname% must be defined before usage. In Windows the command line env variables are case insensitive, so you can adapt your code as to be suitable for Linux too. – razvanone Aug 11 '16 at 10:10
  • @DaveWebb, Thanks showing this method. What is the advantage of this method over the `set host=%COMPUTERNAME%` method of setting the computer name to a variable? This seems to be a little more complicated.. – alpha_989 Feb 28 '18 at 18:30
12

I'm using the environment variable COMPUTERNAME:

copy "C:\Program Files\Windows Resource Kits\Tools\" %SYSTEMROOT%\system32
srvcheck \\%COMPUTERNAME% > c:\shares.txt
echo %COMPUTERNAME%
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brakertech
  • 1,467
  • 1
  • 13
  • 20
9

Why not so?:

set host=%COMPUTERNAME%
echo %host%
sth
  • 222,467
  • 53
  • 283
  • 367
maniattico
  • 127
  • 1
  • 1
0

Just create a .bat file with the line

hostname

in it. That's it. Windows also supports the hostname command.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jitter
  • 53,475
  • 11
  • 111
  • 124
-1
 set host=%COMPUTERNAME%
 echo %host%

This one enough. no need of extra loops of big coding.

Ariful Huq
  • 27
  • 1
  • 5
    could you please explain, how this is better (or at least different) than the identical answers from nine and nine-and-a-half year ago? – Stephan Jan 23 '19 at 08:33
  • Sorry. didnt check it was answered long ago.just answered so that future user may get some value from it by ensuring. – Ariful Huq Jan 23 '19 at 08:40