Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Intrepreting text files with VBscript

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3509
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Intrepreting text files with VBscript
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Intrepreting text files with VBscript - 6/23/2005 9:47:32 AM   
  agentof7

 

Posts: 5
Score: 0
Joined: 6/22/2005
From:
Status: offline
Hey,
Say I have a text file with user inputted data; for exaple I have a text file that just says the number 6 or something.

Now how do I use visual basic scripting to read this text file and save that number as a variable called "count"? Furthermore, what if there were several lines of in the text file where each represents a different variable?
 
 
Post #: 1
 
 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!

(in reply to agentof7)
 
 
Post #: 2
 
 Re: Intrepreting text files with VBscript - 6/23/2005 10:55:38 AM   
  agentof7

 

Posts: 5
Score: 0
Joined: 6/22/2005
From:
Status: offline
how can i test this? put the VBscript in an html file? I dont want to test it on the robot until i know for sure it works

(in reply to agentof7)
 
 
Post #: 3
 
 Re: Intrepreting text files with VBscript - 6/23/2005 11:23:15 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
Just cut and paste the script portion into a text file with a name like TestMe.vbs. Cut and paste the sample data into another file. Change the filename in the script to match whatever you save the sample data as. Then type TestMe.vbs at a command prompt (or double click it from explorer) and observe the output.

(in reply to agentof7)
 
 
Post #: 4
 
 Re: Intrepreting text files with VBscript - 6/23/2005 11:33:08 AM   
  agentof7

 

Posts: 5
Score: 0
Joined: 6/22/2005
From:
Status: offline
Actually it works in the machine... THANKS!

(in reply to agentof7)
 
 
Post #: 5
 
 Re: Intrepreting text files with VBscript - 6/23/2005 11:38:05 AM   
  agentof7

 

Posts: 5
Score: 0
Joined: 6/22/2005
From:
Status: offline
Hey again, while you are on fire with all the coding stuff, is there anway a VBscript can take in script in a text file?

Say there is a text file that has some vbscript in it, can i make a script that can access this text file and execute the script inside the text file?

(in reply to agentof7)
 
 
Post #: 6
 
 Re: Intrepreting text files with VBscript - 6/23/2005 4:29:41 PM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
There are a couple of ways to have a script execute code contained in other files. Here's one way (probably the most useful one)... (BTW: the code doesn't have to come from a file... you can build strings of code dynamically from within your script, or prompt for it with InputBox, and execute it directly!)

Dim aGlobalVariable 'this variable is accessible to any/all the scripts being executed below

Execute GetFileContents("C:\Program Files\MyScripts\Script1.vbs")
Execute GetFileContents("C:\Program Files\MyScripts\Script2.txt")
Execute GetFileContents("C:\Program Files\MyScripts\Script3.WhoCares")

Function GetFileContents(sFileName)
'Note: This function is accessible to any/all the scripts being executed above
' and the functions/subs from those scripts are accessible to this function.
' But, these variables defined inside here are not. The scripts being
' executed obey the same "scope" rules as if the native code they contain
' were directly part of this script file itself. If you want to "hide"
' the outer variables contained in those scripts from the subs/functions of
' this script then place the EXECUTE statements above inside a subroutine.
' Then, the scope is limited to that subroutine.
Dim FSO, oFile
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(sFilename,1)
GetFileContents = oFile.ReadAll
Set oFile = Nothing
Set FSO = Nothing
End Function

Here are a couple of links to other examples/information: http://www.windowsitpro.com/Article/ArticleID/8979/8979.html?Ad=1
http://blogs.msdn.com/ericlippert/archive/2003/09/20/53058.aspx
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsstmExecute.asp

Another way (I'm not providing any examples here but if you need one let me know and I'll put a simple one together.) is to have the first script "shell.run" the CSCRIPT command passing it the name of a second VBS file. Note that when doing it this way that the secondary files MUST have the proper file extensions and that their variables, functions, and subs are not shared among the participating scripts... each script is totally isolated from all the others. The invoking scripts can pass "arguments" to each other via the command line but there is no easy way to receive any "return" values.

(in reply to agentof7)
 
 
Post #: 7
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Intrepreting text files with VBscript Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts