InterviewSolution
| 1. |
Solve : VBScript - Subtract one text file value from another? |
|
Answer» Only me! 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(..) 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
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.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. |
|