Login | |
|
 |
Re: Intrepreting text files with VBscript - 6/23/2005 10:43:48 AM
|
|
 |
|
| |
Xandros
Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
|
Here's a short esample of one way to do it... there are other methods, too, depending on the type of problem you are trying to solve (it's possible for a script to dynamically create variables and execute create/execute other code, too). Option Explicit Dim InputFile Dim FSO, oFile, LineIn, LineNo Dim strData Dim TheVar, SomeVar1, SomeVar2, SomeVar3 InputFile = "E:\data\InputFile.txt" Set FSO = CreateObject("Scripting.FileSystemObject") Set oFile = FSO.OpenTextFile(InputFile) LineNo = 0 Do Until oFile.AtEndOfStream LineIn = oFile.ReadLine LineNo = LineNo + 1 If Left(Trim(LineIn), 1) <> "'" Then ' if line isn't a comment, process it... strData = Split(LineIn, "=") If UBound(strData) > 0 Then ' do we have at least 2 fields? strData(0) = Trim(strData(0)) ' trim the first field strData(1) = Trim(strData(1)) ' trim the second field TheVar = UCase(strData(0)) ' upper case the variable name for comparison Select Case TheVar Case "SOMEVAR1" SomeVar1 = strData(1) Case "SOMEVAR2" SomeVar2 = strData(1) Case "SOMEVAR3" SomeVar3 = strData(1) Case Else Wscript.Echo "Unknown variable found... Line # " & LineNo & " " & strData(0) End Select End If End If Loop oFile.Close Set FSO = Nothing Set oFile = Nothing Wscript.Echo "Last values that were assigned to the variables..." Wscript.Echo "SomeVar1 = " & SomeVar1 Wscript.Echo "SomeVar2 = " & SomeVar2 Wscript.Echo "SomeVar3 = " & SomeVar3 'EndOfScript ' Here is sample content of InputFile.txt... somevar1 = Happy Birthday somevar3 = 456 'somevar3 = I am a commented line somevar999 = non existant somevar1 = I've been reassigned!
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|