Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Divide 2 variables

Posted on 2004-10-29
10
Medium Priority
?
1,036 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:vico1
  • 5
  • 4
9 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12445226
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
 
LVL 15

Author Comment

by:vico1
ID: 12445720
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12459440
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Author Comment

by:vico1
ID: 12460131
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12461864
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
 
LVL 15

Author Comment

by:vico1
ID: 12462393
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12462651
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
 
LVL 15

Author Comment

by:vico1
ID: 12463043
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
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 500 total points
ID: 12483214
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question