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
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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