Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: "Eval" in MS-DOS batch file?  (Read 25960 times)

0 Members and 1 Guest are viewing this topic.

kevinpauli

    Topic Starter


    Starter

    "Eval" in MS-DOS batch file?
    « 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?


    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: "Eval" in MS-DOS batch file?
    « 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.
    I was trying to dereference Null Pointers before it was cool.

    kevinpauli

      Topic Starter


      Starter

      Re: "Eval" in MS-DOS batch file?
      « Reply #2 on: February 16, 2009, 10:33:44 AM »

      C:/>set /a hname = hostname
      0
      C:/>echo %hname%
      0

      kevinpauli

        Topic Starter


        Starter

        Re: "Eval" in MS-DOS batch file?
        « 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%

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: "Eval" in MS-DOS batch file?
        « 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.
        I was trying to dereference Null Pointers before it was cool.

        Dias de verano

        • Guest
        Re: "Eval" in MS-DOS batch file?
        « 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 »

        rets0815

        • Guest
        Re: "Eval" in MS-DOS batch file?
        « 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.