Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General / October 2004



Tip: Looking for answers? Try searching our database.

Reading a text file to populate a variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GM - 31 Oct 2004 00:40 GMT
I have a script that has two systems hard coded in the script.

\\cpu1\......
\\cpu2\......

I want to be able to have my script go out and read a flat textfile for the
cpu information so that in the future I will not have to change hard coding.
Just a text file. So I would have one line

\\namefromtextfile\.......

and this would reffernce the text file and populate it the first time with
CPU1 and then go back and populate it with CPU2 etc.

Can someone help me out with this or point me in the direction of a website
that will help me with this. I would greatly appreciate it.

Thanks
GM
Jezebel - 31 Oct 2004 00:57 GMT
What are you having problems with? Reading a text file is well-covered in
help.

> I have a script that has two systems hard coded in the script.
>
[quoted text clipped - 15 lines]
> Thanks
> GM
GM - 31 Oct 2004 01:33 GMT
I guess what my problem is how do i get it to read line one populate the line
continue with the script and then go back and start the script over and have
it read the second line in the text file.

Thanks
GM

> What are you having problems with? Reading a text file is well-covered in
> help.
[quoted text clipped - 21 lines]
> > Thanks
> > GM
Jezebel - 31 Oct 2004 01:48 GMT
What goes wrong? Perhaps if you post your code ....

> I guess what my problem is how do i get it to read line one populate the line
> continue with the script and then go back and start the script over and have
[quoted text clipped - 28 lines]
> > > Thanks
> > > GM
GM - 31 Oct 2004 03:19 GMT
This is simple but it just reurns the last line of the textfile. Thanks for
your help.

GM

Function ReadLineTextFile()
  Const ForReading = 1, ForWriting = 2
  Dim fso, MyFile
  Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForReading)
   

     Do Until MyFile.atendofline
  ReadLineTextFile = MyFile.ReadLine    ' Returns "Hello world!"
  MsgBox MyFile.ReadLine
   Loop

End Function

> What goes wrong? Perhaps if you post your code ....
>
[quoted text clipped - 35 lines]
> > > > Thanks
> > > > GM
Jezebel - 31 Oct 2004 05:00 GMT
No wonder you're getting confused if you're using the FSO for this. Stick to
VB's native methods. Open the file, read your variables, close the file.
That really is all there is to it.

Check Help on the Open and Input methods.

> This is simple but it just reurns the last line of the textfile. Thanks for
> your help.
[quoted text clipped - 53 lines]
> > > > > Thanks
> > > > > GM
Tom_OM - 31 Oct 2004 06:24 GMT
I agree with Jezebel.  I would junk the use of the FileSystemObject.
FSO is seductive because it's easy to use and you can do a lot with
very few lines of code.  However, it can easily come back to bite you
in the butt because there are so many different versions of FSO from
operating system to operating system.  

Also, when you declare your variables like "Dim fso, MyFile" without
specifying a data type, VB automatically makes them variants.
Variants are the most resource hungry of any data type and can also
come back to bite you in the butt.  

A lot of my progs save and retrieve variables to and from text files
all the time.  In your case I would just save those two lines as two
string variables and retrieve them as such later when you need them.
Here are two subs of mine that I use and reuse for storing variables.
They store many more than the two strings that you need to use and
store some other datatypes, but you could pare them down to easily
make them store and retrieve the two strings that you're using.  When
you're using the "SaveVariables" sub, the first string you feed it is
used to name the file.  Then if you feed the RetrieveVariables sub the
same string, it opens the same file to grab the saved data.  For
example, if you feed SaveVariables "allthestuff", it saves it in a
file named "allthestuff.dat".  

I'm also including a stock Function that one of the subs relies on.  I
hope this is helpful.  

'---------------------------------------------------------------------------------------
' Procedure : SaveVariables
' Purpose   : Save variables to a file for later retrieval.
'           : Sub can be used over and over called by many different
subs.
'           : Use in conjunction with the "RetrieveVariables" Sub.
'---------------------------------------------------------------------------------------
'
Public Sub SaveVariables(ByVal strFileName As String, _
                       ByVal str1stString As String, ByVal
str2ndString As String, _
                       ByVal str3rdString As String, ByVal
str4thString As String, _
                       ByVal str5thString As String, ByVal int1stInt
As Integer, _
                       ByVal int2ndInt As Integer, ByVal int3rdInt As
Integer, _
                       ByVal bln1stBoo As Boolean, ByVal bln2ndBoo As
Boolean, _
                       ByVal bln3rdBoo As Boolean)
Dim intFF As Integer

' kill previous hold file if it exists
If FileExists(App.Path & "\" & strFileName & ".dat") Then
   Kill App.Path & "\" & strFileName & ".dat"
End If

intFF = FreeFile
Open App.Path & "\" & strFileName & ".dat" For Output As #intFF
   Write #intFF, str1stString, str2ndString, str3rdString,
str4thString, str5thString, _
   int1stInt, int2ndInt, int3rdInt, bln1stBoo, bln2ndBoo, bln3rdBoo
   Close #intFF
End Sub

'---------------------------------------------------------------------------------------
' Procedure : RetrieveVariables
' Purpose   : Retrieve variables that have been saved to sequential
access file.
'           : Use in conjunction with the "SaveVariables" Sub.
'---------------------------------------------------------------------------------------
'
Public Sub RetrieveVariables(ByRef strFileName As String, _
                       ByRef str1stString As String, ByRef
str2ndString As String, _
                       ByRef str3rdString As String, ByRef
str4thString As String, _
                       ByRef str5thString As String, ByRef int1stInt
As Integer, _
                       ByRef int2ndInt As Integer, ByRef int3rdInt As
Integer, _
                       ByRef bln1stBoo As Boolean, ByRef bln2ndBoo As
Boolean, _
                       ByRef bln3rdBoo As Boolean)
Dim intFF As Integer

intFF = FreeFile

Open App.Path & "\" & strFileName & ".dat" For Input As intFF

Do While Not EOF(intFF)
   Input #intFF, str1stString, str2ndString, str3rdString,
str4thString, str5thString, _
   int1stInt, int2ndInt, int3rdInt, bln1stBoo, bln2ndBoo, bln3rdBoo
Loop

Close #intFF

End Sub

Public Function FileExists(strCheckEe As String) As Boolean
Dim strCheck_File As String
   strCheck_File = Dir(strCheckEe)
   If Len(strCheck_File) > 0 Then
       FileExists = True
   Else
       FileExists = False
   End If
End Function
J French - 31 Oct 2004 13:41 GMT
<snip>

>Public Function FileExists(strCheckEe As String) As Boolean
>Dim strCheck_File As String
>    strCheck_File = Dir(strCheckEe)
     Just don't do that Dir() is dangerous here

>    If Len(strCheck_File) > 0 Then
>        FileExists = True
>    Else
>        FileExists = False
>    End If
>End Function
DanS - 31 Oct 2004 16:40 GMT
erewhon@nowhere.uk (J French) wrote in news:4184ea69.177687117
@news.btclick.com:

><snip>
>
[quoted text clipped - 9 lines]
>>    End If
>>End Function

There's also the option of using an INI file instead of just a flat text
file. Then you could read any data saved under a setting group easily,
without having to be concerned about the order of the data in the text
file.

DanS
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.