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
LVL 15
vico1CIOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimbobmcgeeCommented:
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.
0
vico1CIOAuthor Commented:
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.


0
jimbobmcgeeCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

vico1CIOAuthor Commented:
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





0
jimbobmcgeeCommented:
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.
0
vico1CIOAuthor Commented:
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


0
jimbobmcgeeCommented:
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.
0
vico1CIOAuthor Commented:
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
0
jimbobmcgeeCommented:
Try this:

      Dim objWSH, objFSO, objTS, varTDIS, varCAC

      set objWSH = CreateObject("WScript.Shell")
      set objFSO = CreateObject("Scripting.FileSystemObject")

      set objTS = objFSO.OpenTextFile("d:\test\cac.txt")
          varCAC = Trim(objTS.ReadLine)
      objTS.Close

      set objTS = objFSO.OpenTextFile("d:\test\tdis.txt")
          varTDIS = Trim(objTS.ReadLine)
      objTS.Close

      set objTS = objFSO.CreateTextFile("d:\test\pcid.txt", True)
          objTS.Write varTDIS / varCAC
      objTS.Close

      set objTS = Nothing

it will read from two files, divide the contents and return the result.  It won't error-check, you'll just have to see if the file exists.  Remember to make the batch file give the script time to run...

HTH

J.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.