Link to home
Start Free TrialLog in
Avatar of Murat Raymond
Murat RaymondFlag for United States of America

asked on

Divide 2 variables

Hi Experts!

I created a small Batch file to colllect info from a few text files.

I need to call a vb script within the batch file that can divide 2 variables and set the result to a variable.

Example:
suppose my batch file has these two variables: TDIS=556.76 and CAC=45

I need to call a VB script that can divide %TDIS% by %CAC% and get the result as PCDI=12.37.

I could not do it with the command set /a  

Thank you in advance for your help.

Vico1
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

The following will do it (but the resulting environment variable is deleted when the script finishes):

      Dim objWSH, a, b, c

      set objWSH = CreateObject("WScript.Shell")

      a = objWSH.Environment.Item("tdis")
      b = objWSH.Environment.Item("cac")

      msgbox a, , b

      c = a / b

      objWSH.Environment.Item("pcdi") = c

      objWSH.Run "%COMSPEC%", , True

      msgbox "done"

HTH

J.
Avatar of Murat Raymond

ASKER

It did not work. However this is what I can do:

I can redirect the variables to a text file from the batch file something like:

echo %tdis%>tdis.txt
echo %cac%>cac.txt

I could redirect your vb Script from the batch file:

echo Dim objWSH, a, b, c>temp.vbs
echo set objWSH = CreateObject("WScript.Shell")>>temp.vbs
echo (You get the idea)

then
call the temp.vbs from the batch
If the result on the vbscript can be redirected to a text file like pcdi.txt I can read the text file and load in the environment.

I hope that helped

I don know anything about VB otherwise I could play with it.


When you say, it didn't work -- what exactly didn't work?  The above code was designed to open a cmd window when it was done -- did you type 'set' in that window to see if it had stored a value in %pcdi%?

Can you change your batch file to use %1 instead of %pcdi%?  If so, you could run your batch file with a parameter:

     mybatchfile.bat 12.37

Then you could use the following:

     Dim objWSH, a, b, c

     set objWSH = CreateObject("WScript.Shell")

     a = objWSH.Environment.Item("tdis")
     b = objWSH.Environment.Item("cac")

     c = a / b

     objWSH.Run "mybatchfile.bat " & c, , True

     msgbox "done"

HTH

J
I copied and paste your codes to a file and called it cal.vbs
then called it from the batch.
I get the following error:

lines:    10
Char:    6
error:   Type mismatch: '[string: ""]'
code:    800A00D
source: Microsoft VBScript runtime error

Maybe I did it wrong.
This is a sample of What i would need to do:

@echo off
set /p cac=
set /p tdis=

call cal.vbs

echo %pcdi%

Basically I need to divide the two variables.

If I try the command: set /a pcdi=%tdis%/%cac%

If the number is decimal I get the wrong answer.
example if I divide  233.48 by 54, I will get the wrong answer loaded.
all I need is a way to divide these two variables.
Even the result output is a text file that would be OK.

thanks





I don't understand where the error came from -- both have worked fine with me.

If you just want to display the result of tdis / cac, then just use:

     Dim objWSH
     set objWSH = CreateObject("WScript.Shell")
     WScript.Echo objWSH.Environment.Item("tdis") / objWSH.Environment.Item("cac")

If you want to write to an environment variable, I can't raise any error with:

     Dim objWSH
     set objWSH = CreateObject("WScript.Shell")
     objWSH.Environment.Item("pcdi") = objWSH.Environment.Item("tdis") / objWSH.Environment.Item("cac")

If you want to write to a file:

     Dim objWSH, objFSO, objTS
     set objWSH = CreateObject("WScript.Shell")
     set objFSO = CreateObject("Scripting.FileSystemObject")
     
     set objTS = objFSO.CreateTextFile("x:\mypath\myfile.dat", True)
     objTS.Write objWSH.Environment.Item("tdis") / objWSH.Environment.Item("cac")
     objTS.Close

HTH

J.
I hate to tell you, I didn't work
I did the following short batch to test it

@echo off
::HERE I LOADED THE VRIABLES
SET /P TDIS=
SET /P CAC=

:: THEN CALL YOUR CODES VIA THE FILE CAL.VBS
CALL CAL.VBS  
::( ANY OF YOUR CODES)

::THEN DISPLAY THE RESULT
IF DEFINED PCDI ECHO %PCDI%
IF EXIST D:\RES.DAT TYPE RES.DAT


copy and paste it you will see the error.

Thanks

Vico1


OK, I think I understand the problem...

When Windows opens an old DOS-type system -- a cmd prompt or batch file -- it takes the environment variables, stored within Windows, and copies them to the instance of that DOS entity.  When you use the SET command, in the DOS entity, it does not transfer back to the Windows environment.

The VBS Enviroment.Item is stored within the Windows environment settings -- that's just the way it is -- it will not update within any open DOS entities; it will only be copied in any new DOS entities.

However, when the VBS attempts to read the values (to divide them), it reads from the existing Windows ones.  If they don't exist, it will not work.

Bottom line is, we need to work out another way to do what you are trying to do.

So, what exactly do you need this batch file and these environment variables for?

J.
If I can redirect these environment to a text file like:

TDIS.TXT and CAC.TXT

Can you redirect the result to a text file like PCDI.TXT?

The point is the the batch ask a few questions to collect Numbers, then do math operation for average and thresold to return Colors like Red ,Green or yellow.

everything work fine until that  a Decimal number or a negative number is entered ex:(144.55, -96)

I know it would have been easier to do it in VB all together, But I am not that good yet.

Thanks

vico1
ASKER CERTIFIED SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial