Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading a text file

Status
Not open for further replies.

lavadan

Programmer
Dec 17, 2007
49
US
Hi all,

I have a text file test.txt.
I have two lines in the test file
c:\name.txt
\\11.23.1654

Can i write a script so that the code has to read the test file and assign the values in the test file to different variables

i.e the output should be
src=c:name.txt
src1=\\11.23.1654

How can i do this in vb script. Please help me as i am new to vb script

 
Look at the documentation for the FileSystemObject. Specifically the .OpenTextFile method. And the File Object's .ReadLine and .ReadAll methods. Searching this forum for those keywords will also get you plenty of samples of reading data from files.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi,

I read the documentation. It says how to read a text file. But i want to read a text file and assign each line of the text file to a variable. How can i read each line.
 
I would suggest reading the documentation for ReadLine again. This is copied directly from it:

Set MyFile = fso_OpenTextFile("c:\testfile.txt", ForReading)
ReadLineTextFile = MyFile.ReadLine ' Returns "Hello world!"


It assumes that you have a text file named C:\testfile.txt and that the first line in the file is "Hello world!".

To get more specific examples I would suggest searching this forum.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi EBGreen,

Thanks for your suggestion. I am able to read the text file.

My question is how to read the second line in the text file?
 
Have you searched the forum? This has been covered lots of times.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Yeah. I have gone through the forum.
I am able to read the text file but i want to read the text file based on the lines and assign them to variables.

i.e the script should assign
fist line c:\test.txt to src
and the second line,the path 11.23.1654 to src1

Can anybody please help me with this as i am new to scripting.

 
Ok, well if you have searched the forum then you have seen the answer. You just didn't know that you saw the answer.

There are two methods for doing this. One reads the entire file as one big slurp and assigns each line as an item in an array. The other method opens the file then goes through the file one line at a time doing whatever you want with each line. I will show you the slurp method first and we can worry about the other method later if you need it.

Let's say you have a file named C:\foo.txt

Here is the code to get the contents of that file into an array:

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")

arrLines = Split(oFSO.OpenTextFile("C:\foo.txt").ReadAll(), VbCrLf)

Now you have all the lines in the file in an array, so if you want src to be the first line and src1 to be the second line (not a fan of those names by the way), simply do this:

src = arrLines(0)
src1 = arrLines(1)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks EBGreen. I got an idea now.
I will proceed now and will get back if i have any questions.
 
Just call the ReadLine() method once for each line you want to read. There is usually no reason and little gain in complicating matters.
 
I thought about suggesting simply using ReadLine() twice, but this:

"I have a text file test.txt.
I have two lines in the test file"

Made me think that a more generalized solution might be better.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top