Home / Microsoft / Microsoft DOS / "Eval" in MS-DOS batch file?
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: "Eval" in MS-DOS batch file?  (Read 6269 times)
kevinpauli
Topic Starter
Starter



Posts: 3


« on: February 16, 2009, 10:20:29 AM »

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?

IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,881

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #1 on: February 16, 2009, 10:21:44 AM »

set /a can be used for doing arithmetic, I think it works for strings to.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
kevinpauli
Topic Starter
Starter



Posts: 3


« Reply #2 on: February 16, 2009, 10:33:44 AM »


C:/>set /a hname = hostname
0
C:/>echo %hname%
0
IP logged
kevinpauli
Topic Starter
Starter



Posts: 3


« Reply #3 on: February 16, 2009, 10:44:42 AM »

I found something that works.  Ugly, but it works.

@echo off
for /f "tokens=* delims= " %%a in ('hostname') do (
set HOST=%%a
)
echo %HOST%
IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,881

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #4 on: February 16, 2009, 10:48:02 AM »

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.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Dias de verano
Guest
« Reply #5 on: February 16, 2009, 11:50:33 AM »

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



« Last Edit: February 16, 2009, 12:50:49 PM by Dias de verano » IP logged
rets0815
Newbie



Posts: 1


« Reply #6 on: June 12, 2009, 09:03:54 AM »

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.
IP logged
Pages: [1] - (Top) Print 
Home / Microsoft / Microsoft DOS / "Eval" in MS-DOS batch file? « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.1 seconds with 20 queries.