1.

Solve : VBScript - Subtract one text file value from another?

Answer»

Only me!

I've just moved the bed in as I can see this being my second home now!

I have 2 text files with one numerical value each (i.e. 54338). I would like to get another script to take one value and subtract it from the other, and have this resultant value written to a new (3rd) text file.

what I have is:

Const ForReading = 1

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("..\freespacebefore.txt", ForReading)
objFile1.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO.OpenTextFile("..\freespaceafter.txt", ForReading)
objFile2.Close

Set objFile3 = objFSO.CreateTextFile("..\savedspace.txt")
objFile3.WriteLine "savedspace=" & (objFile2-objFile1)
objFile3.Close


The error message I am getting is 'Object doesn't support this property or method' - refering to the highlighted text.Opening a file does not get you any data. You'll need to read the file.

Code: [Select]Const ForReading = 1

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("..\freespacebefore.txt", ForReading)
value1 = objFile1.ReadLine
objFile1.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO.OpenTextFile("..\freespaceafter.txt", ForReading)
value2 = objFile2.ReadLine
objFile2.Close

Set objFile3 = objFSO.CreateTextFile("..\savedspace.txt")
objFile3.WriteLine "savedspace=" & (value2 - value1)
objFile3.Close

Thank you for your speedy response, much appreciated.

I am getting an error message from the code:

Type mismatch: 'value2'Quote

I have 2 text files with one numerical value each (i.e. 54338). I would like to get another script to take one value and subtract it from the other,

Probably need to parse the data.

Code: [Select]Const ForReading = 1

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("..\freespacebefore.txt", ForReading)
arrData1 = Split(objFile1.ReadLine, " ")
objFile1.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO.OpenTextFile("..\freespaceafter.txt", ForReading)
arrData2 = Split(objFile2.ReadLine, " ")
objFile2.Close

Set objFile3 = objFSO.CreateTextFile("..\savedspace.txt")
objFile3.WriteLine "savedspace=" & (arrData2(0) - arrData1(0))
objFile3.Close

Based on each numerical value followed by a space.



Curious: What is strComputer?Sidewinder, would Val() not perform the same function as your usage of Split()?It might have, but val() is not listed as a valid function in the VBSCRIPT documentation! Using the val function throws a type mismatch error.

VBScript Documentation



I'm still getting a Type mismatch: 'arrData2(..)

The strComputer"." - applies it to the local machine. Just something I have been doing as I''ve also worked on networked computers with vbscript.Quote from: Sidewinder on SEPTEMBER 24, 2008, 07:10:19 AM
It might have, but val() is not listed as a valid function in the VBScript documentation!


hmm, I guess VB6 wins this round

I guess instead of having explicit conversion function, they decided to have VBScript always USE Evil Type coercion, since EVERYTHING is a Variant.



Quote from: pcwizard on September 24, 2008, 07:16:02 AM
I'm still getting a Type mismatch: 'arrData2(..)

The strComputer"." - applies it to the local machine. Just something I have been doing as I''ve also worked on networked computers with vbscript.

hmm, weird, it works for me.

but whats with the use of .. for parent directory?PCWizard: Please post the data from the freespacebfore and freespaceafter files. I cannot duplicate your error.

Quote

I guess instead of having explicit conversion function, they decided to have VBScript always use Evil Type coercion, since everything is a Variant.

VBScript was designed for non-programmers. With everything a variant (except date/time), there was no need for the user to get bogged down with data types and worrying about conversions. Let the interpreter do the work based on real live data!



Let me clarify, VBScript does something that in Visual Basic is referred to as Evil Type coercion. In the context of a script language it makes sense and has a greatly reduced evilness, but it adds to the confusion when my Scripts constantly fail because of my insistence to automatically put a type. Or even worse, TRY to use objects from VB- like the PropertyBag and such.
Or try to do both at once!

Code: [Select]Dim PropBag as New PropertyBag
Quote
but whats with the use of .. for parent directory?

I'm using Flash as a front end, so containing all of the code samples and scripts in the fscommand folder. But the text files remain outside of this because the content is being accessed by flash again afterwards.Quote from: Sidewinder on September 24, 2008, 07:35:33 AM
PCWizard: Please post the data from the freespacebfore and freespaceafter files. I cannot duplicate your error.

Quote

following your request I have found the issue is indeed with the content from the 2 text files.

I have freespace after as "freespaceafter=123456"
I have freespacebefore as "freespacebefore=1234567"

I see now that the variables freespace..... are causing the issue.

I need these to remain though as this is what flash uses to collect the data. I should however be able to get vbscript to create duplicate freepsace... files without this text in.

Thanks so much for everyone's help.
Not sure if you found the solution, but this might help:

Code: [Select]Const ForReading = 1

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("..\freespacebefore.txt", ForReading)
arrData1 = Split(objFile1.ReadLine, "=")
objFile1.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO.OpenTextFile("..\freespaceafter.txt", ForReading)
arrData2 = Split(objFile2.ReadLine, "=")
objFile2.Close

Set objFile3 = objFSO.CreateTextFile("..\savedspace.txt")
objFile3.WriteLine "savedspace=" & (arrData2(1) - arrData1(1))
objFile3.Close

Happy coding.


Discussion

No Comment Found