1.

Solve : "Eval" in MS-DOS batch file??

Answer»

Sorry if this is a very basic question, but surprisingly, extensive googling on the topic has yielded nothing of value.

How do I do an "eval" in MS-DOS?

The task at hand is to capture the current hostname in a variable so I can use it elsewhere in the script. I found the "hostname" command, which when executed spits out the hostname. What I want to do is grab the output of the hostname command and store in in a variable.

I am thinking there must be some kind of directive in the batch file, either special characters surrounding the expression, or some kind of a switch, to say "don't treat this string as a LITERAL, INSTEAD EVALUATE it and return me the result".

Help?

set /a can be USED for doing arithmetic, I think it works for strings to.
C:/>set /a hname = hostname
0
C:/>echo %hname%
0
I found something that works. Ugly, but it works.

@echo off
for /f "tokens=* delims= " %%a in ('hostname') do (
set HOST=%%a
)
echo %HOST%I thought you wanted to evaluate an expression- not redirect the output of a command into a variable. Although rereading your original post makes it quite clear this was your intention. Sorry if I MISLED you.1. You can get it all on one line.

Code: [Select]for /f "tokens=* delims= " %%a in ('hostname') do set HOST=%%a
2. The tokens/delims block looks a little odd. As in redundant. Spaces as delims, when they are the default, and I think host names don't have any spaces anyway, so the tokens bit is redundant too, but I am ready to be corrected.

3. On my XP system, you can get the same result, but with alpha chars in upper case, by merely expanding the system variable %userdomain% but maybe that won't be true for all systems?

Code: [Select]C:\>for /f %a in ('hostname') do @echo %a
pupp-c92f25ed23

C:\>echo %userdomain%
PUPP-C92F25ED23



Code: [Select]C:\>echo %userdomain%
PUPP-C92F25ED23

the above command will not give you the hostname, instead it will print the domain. So I guess you use a local account for logging into your computer.



Discussion

No Comment Found