BATCH script variable woes!

Started by
4 comments, last by mrmrcoleman 16 years, 9 months ago
Hello, I am trying to get the result of a call to hostname in DOS into a variable. Typing hostname on the command line give me PC-COLEMANM which is exactly what I want, but I need to be able to put this in a variable. So far I have tried: SET HOST=hostname SET HOST="hostname" SET HOST=`hostname` SET HOST='hostname' And all of the above with '.exe' appended. Unfortunately instead of getting the result of the hostname call I just get whatever is on the right hand side of the equals sign. Does anybody know how I can get this working? Kind regards, Mark
Advertisement
DOS batch scripting is the worst thing ever.

This is the only way I know to make it work:

for /f %i in ('hostname') do (set HOST=%i)

In a script, you'll need to double up one of the percentage signs, or possibly both, although you should not when you do it from the command line.

I told you - worst thing ever.
I actually couldn't agree more about DOS batch scripting being god awful, but unfortunately I am stuck with it as I can't do what I need to with Cygwin.

The code worked, and you were right about the extra %i, it was needed at the end.

Big thanks,

Mark
You could use a real language perhaps? Like ruby or python etc...
Batch files are so much easier to work with on NT based systems - NT/W2K/XP

There are a ton of batch file examples here: Rob van der Woude's Scripting Pages.

Maybe one of them does what you want.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
rip-off: I don't know those languages and this is a very small script.

LessBread: Thanks for the link.

This topic is closed to new replies.

Advertisement