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

Author Topic: Possibly handy VBS script  (Read 4787 times)

0 Members and 1 Guest are viewing this topic.

Dias de verano

  • Guest
Possibly handy VBS script
« on: February 21, 2009, 03:38:47 AM »
Win2K onwards...

Code: [Select]
Wscript.echo eval(WScript.Arguments(0))
Save this script as Evaluate.vbs

Now, (cumbersome) either call it by

cscript //nologo "Path to\evaluate.vbs" "valid VBS expression" [quotes not always needed but advised]

Code: [Select]
C:\>cscript //nologo "c:\utils\evaluate.vbs" "hour(time)"
10

C:\>cscript //nologo "c:\utils\evaluate.vbs" "2/3"
0.666666666666667

C:\>cscript //nologo "c:\utils\evaluate.vbs" "time"
10:17:54

C:\>cscript //nologo "c:\utils\evaluate.vbs" "now"
21/02/2009 10:18:00

or...

(less cumbersome to use)

1. Save it somewhere on your PATH, with a name that does not duplicate any existing executable on your PATH.

2. Set cscript as the default vbs engine by executing cscript //H:cscript

3. Turn off the logo display by executing cscript //nologo /S

(These settings are saved and will persist through reboots until you alter them as described in cscript /?)

Now you can do this

Code: [Select]
C:\>evaluate "1/9"
0.111111111111111

C:\>evaluate "(100/2)*3"
150

C:\>evaluate "100/(2*3)"
16.6666666666667

C:\>evaluate "2^16"
65536

C:\>evaluate "4*atn(1)"
3.14159265358979

C:\>evaluate now
21/02/2009 10:26:24

C:\>evaluate "(4*atn(1))*(5^2)"
78.5398163397448

and in a batch... (watch out for the ^ operator - use 1 at the prompt, 2 in a batch file)

Code: [Select]
@echo off
set radius=5
set pi=(4*atn(1))
set expression=%pi%*(%radius%^^2)
for /f "delims==" %%A in ('evaluate "%expression%" ') do set answer=%%A
set area=%answer%
echo Circle radius: %radius%
echo Circle area:   %area%
« Last Edit: February 21, 2009, 07:35:30 AM by Dias de verano »

macdad-



    Expert

    Thanked: 40
    Re: Possibly handy VBS script
    « Reply #1 on: February 21, 2009, 07:55:37 AM »
    nice script Dias,

    does it also work with batch variables?
    If you dont know DOS, you dont know Windows...

    Thats why Bill Gates created the Windows NT Family.

    Dias de verano

    • Guest
    Re: Possibly handy VBS script
    « Reply #2 on: February 21, 2009, 08:18:16 AM »
    nice script Dias,

    does it also work with batch variables?

    Not sure what you mean. You can feed batch variables into it. In a batch you could invoke it directly if you just wanted to show the answer or you could use FOR to get the answer into a batch variable. Above, I showed it evaluating a batch string variable like this:

    Code: [Select]
    set pi=(4*atn(1))
    set expression=%pi%*(%radius%^^2)
    for /f "delims==" %%A in ('evaluate "%expression%" ') do set answer=%%A
    set area=%answer%

    or...

    Code: [Select]
    C:\>set n1=6

    C:\>set n2=5

    C:\>evaluate %n1%+%n2%
    11

    BatchFileCommand



      Hopeful
    • Thanked: 1
      Re: Possibly handy VBS script
      « Reply #3 on: February 21, 2009, 09:49:22 AM »
      Quote
      ^ operator

      What does the ^ operator do?
      οτη άβγαλτος μεταφ βαθμολογία

      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: Possibly handy VBS script
      « Reply #4 on: February 21, 2009, 10:01:42 AM »
      Exponentiation. I think it escapes characters in batch so I think that has something to do with it.


      I still say BCScript and BASeParser are better. They can, after all, do this:

      Code: [Select]
      STORE(X,{25,36,49})
      STORE(Y,Sqr(-X)+X[{1,2,3} PICK 1])
      BCSH@WriteLn(Y)
      which would output(possibly, given use of random PICK operator):

      Code: [Select]
      {5i+25,6i+49,7i+36}


      Supports Imaginary numbers, as well as intrinsic support for matrix operations and so forth- not to mention such novel operators such as the "PICK" operator used previous as well as nCr and nPr (number of Combinations and Number of Permutations, respectively) And ** or ^ as multiplication, Logical and boolean operations, etc.

      It's going to be a tough cookie to document, though...

      And I assure you it's not one line of code. Actually:



      15,080 code lines and 6,863 Comment lines.

      So I suppose this method wins in the terseness department...

      I was trying to dereference Null Pointers before it was cool.

      macdad-



        Expert

        Thanked: 40
        Re: Possibly handy VBS script
        « Reply #5 on: February 21, 2009, 10:07:47 AM »
        nice script Dias,

        does it also work with batch variables?

        Not sure what you mean. You can feed batch variables into it. In a batch you could invoke it directly if you just wanted to show the answer or you could use FOR to get the answer into a batch variable. Above, I showed it evaluating a batch string variable like this:

        Code: [Select]
        set pi=(4*atn(1))
        set expression=%pi%*(%radius%^^2)
        for /f "delims==" %%A in ('evaluate "%expression%" ') do set answer=%%A
        set area=%answer%

        or...

        Code: [Select]
        C:\>set n1=6

        C:\>set n2=5

        C:\>evaluate %n1%+%n2%
        11

        thats what i meant...but its pretty good.  8)
        If you dont know DOS, you dont know Windows...

        Thats why Bill Gates created the Windows NT Family.

        Dusty



          Egghead

        • I could if she would, but she won't so I don't.
        • Thanked: 75
        • Experience: Beginner
        • OS: Windows XP
        Re: Possibly handy VBS script
        « Reply #6 on: February 21, 2009, 06:11:58 PM »
        Really handy - floating point math in batch scripting is a step closer (almost).

        Thanks Dias.
        One good deed is worth more than a year of good intentions.

        Dias de verano

        • Guest
        Re: Possibly handy VBS script
        « Reply #7 on: February 22, 2009, 02:19:01 AM »
        Exponentiation. I think it escapes characters in batch so I think that has something to do with it.

        And it also can act as a line continuation character, depending on the circumstances. So you can prettify batch scripts where the lines are very long.

        Code: [Select]
        @echo off
        dir ^
        /a-d ^
        /w

        But its role as a batch script control character means it needs escaping in a batch file, by the escape char which happens to be another ^ (caret) character. I have seen people call it a "carrot".



        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: Possibly handy VBS script
        « Reply #8 on: February 22, 2009, 09:18:32 AM »
        I've had people call it an up-arrow. Not exactly accurate, or the Asterisk, "I put my password in and I see a bunch of spiders"  ::)
        I was trying to dereference Null Pointers before it was cool.