Solved

Vbscript: I want to read a string in a text file on a certain line delimited by a certain delimiter

Posted on 2011-03-14
5
409 Views
Last Modified: 2012-05-11
This string starts at the beginning of the line (I have the linenumber) and is delimited with the following delimiter : #.
0
Comment
Question by:itbabe
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:jawa29
ID: 35130037
This code will read eachline of a text file and pop up a window when it finds a line that has # at the start.

Will this give you a base to work on?

Jawa29
Const ForReading = 1

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFSO.OpenTextFile ("TextFileforReading.txt", ForReading)

Do Until oTextFile.AtEndOfStream
    sLine = oTextFile.ReadLine
    If Left(sLine, 1) = "#" Then
    	WScript.Echo sLine
    End If
Loop

Open in new window

0
 
LVL 8

Accepted Solution

by:
GundogTrainer earned 500 total points
ID: 35130240
Using the code posted by jawa29 I think this is what mean:
You have a text file,
you know that say line 10 is delimited with "#" and you want to examine that line.

Something like this should do that.
Const ForReading = 1
LineNumToRead=10 'you said you had the line number
CurrentLineNum=0
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFSO.OpenTextFile ("TextFileforReading.txt", ForReading)

Do Until oTextFile.AtEndOfStream
CurrentLineNum=CurrentLineNum+1
    sLine = oTextFile.ReadLine
    If CurrentLineNum=LineNumToRead then
         ArrLine=split(sLine,"#")
           For arrNum=0 to ubound(ArrLine) 'Loop through the line and display the individaul entries
             WScript.Echo arrNum,ArrLine(arrNum)
           Next
    End If
Loop

Open in new window

0
 

Author Comment

by:itbabe
ID: 35130267
Thanks for the help.
I need to be able to provide the line number at whic it should search for the string.
How can I achieve that ?
0
 
LVL 8

Expert Comment

by:GundogTrainer
ID: 35130297
Change LineNumToRead to the line number to inspect.
0
 
LVL 8

Expert Comment

by:jawa29
ID: 35130299
This one will read one line given by the paramater iLineNumber

Jawa29
Const FOR_READING = 1

iLineNumber = 5

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFSO.OpenTextFile("TextFileForReading.txt", FOR_READING)

For i=1 To (iLineNumber-1)
   oTextFile.SkipLine
Next

WScript.Echo oTextFile.Readline

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question