Hey all, I'm trying to make a script that searches a text file for a
line, takes the number on that line and sticks in in a variable. I
have been working on it for about an hour with little success. I
figure I need to use the readall method, but is there a way I can get
the readline method to just grab the data off a certain line say like
line 16 so I do not need to search the whole txt? Heres what I have
so far.

im filesys, text, readfile, contents, svrtag
set filesys = CreateObject("Scripting.FileSystemObject")
set readfile=filesys.OpenTextFile("C:\BRUNET\LIB\SET_BRUNET.CFG")

strWordList = readfile.ReadAll
readfile.Close

strSearchWord = "ST_NUM" & vbCrLf

If InStr(strWordList, strSearchWord) = True Then Wscript.echo
strSearchWord


Any help would be greatly appreciated.

Re: Searching a text file for a line. by ekkehard

ekkehard
Fri Dec 07 07:42:20 PST 2007

grout58@gmail.com schrieb:
> Hey all, I'm trying to make a script that searches a text file for a
> line, takes the number on that line and sticks in in a variable. I
> have been working on it for about an hour with little success. I
> figure I need to use the readall method, but is there a way I can get
> the readline method to just grab the data off a certain line say like
> line 16 so I do not need to search the whole txt? Heres what I have
> so far.
>
> im filesys, text, readfile, contents, svrtag
> set filesys = CreateObject("Scripting.FileSystemObject")
> set readfile=filesys.OpenTextFile("C:\BRUNET\LIB\SET_BRUNET.CFG")
>
> strWordList = readfile.ReadAll
> readfile.Close
>
> strSearchWord = "ST_NUM" & vbCrLf
>
> If InStr(strWordList, strSearchWord) = True Then Wscript.echo
> strSearchWord
>
>
> Any help would be greatly appreciated.

Instr() returns the position of the found string or 0 (not found);
so "Instr(...) = True" is bad/wrong.
If you get the position of your strSearchWord in strWordList then
you have to use this position to determine where "then number on that
line" is to be found, before you can use Mid() to cut it and to "stick[s]
in a variable".

It may be easier to use a RegExp instead of Instr(); posting (a part of)
SET_BRUNET.CFG would help me to help you.

Re: Searching a text file for a line. by McKirahan

McKirahan
Fri Dec 07 08:55:22 PST 2007

<grout58@gmail.com> wrote in message
news:c3e22086-60af-490e-8c90-adf03776301a@s8g2000prg.googlegroups.com...
> Hey all, I'm trying to make a script that searches a text file for a
> line, takes the number on that line and sticks in in a variable. I
> have been working on it for about an hour with little success. I
> figure I need to use the readall method, but is there a way I can get
> the readline method to just grab the data off a certain line say like
> line 16 so I do not need to search the whole txt? Heres what I have
> so far.
>
> im filesys, text, readfile, contents, svrtag
> set filesys = CreateObject("Scripting.FileSystemObject")
> set readfile=filesys.OpenTextFile("C:\BRUNET\LIB\SET_BRUNET.CFG")
>
> strWordList = readfile.ReadAll
> readfile.Close
>
> strSearchWord = "ST_NUM" & vbCrLf
>
> If InStr(strWordList, strSearchWord) = True Then Wscript.echo
> strSearchWord


Will this help? Watch for word-wrap.

Change the vlaue of "cFIL" for your filename.
Change the vlaue of "cFND" for your search string.
Use "LCase()" if you want a case-insensitice match.
Modify "strVAL" to the name of your variable.

Option Explicit
'*
Const cFIL = "C:\BRUNET\LIB\SET_BRUNET.CFG"
Const cFND = "ST_NUM"
'*
Dim strVAL
strVAL = LineNumber(cFIL,cFND)
WScript.Echo cFND " is in line " & strVAL & " (0 = not found)"

Function LineNumber(filename,findline)
LineNumber = 0
'*
Dim arrOTF
Dim intOTF
Dim strOTF
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOTF
Set objOTF = objFSO.OpenTextFile(filename,1)
strOTF = objOTF.ReadAll
Set objOTF = Nothing
Set objFSO = Nothing
'*
If InStr(strOTF,findline) > 0 Then
arrOTF = Split(strOTF,vbCrLf)
For intOTF = 0 To UBound(arrOTF)
If InStr(arrOTF(intOTF),findline) > 0 Then
LineNumber = intOTF+1
Exit For
End If
Next
End If
End Function



Re: Searching a text file for a line. by ekkehard

ekkehard
Fri Dec 07 10:58:44 PST 2007

grout58@gmail.com schrieb:
> Hey all, I'm trying to make a script that searches a text file for a
> line, takes the number on that line and sticks in in a variable.
[...]
Reading McKirahan's made me realize then "number on that line" should
be understood as "number of that line". That can be done like this:

Dim oFS : Set oFS =3D CreateObject( "Scripting.FileSys=
temObject" )
Dim s1FSpec : s1FSpec =3D "r2w1File1.txt"
Dim strSearchWord : strSearchWord =3D "ST_NUM" & vbCrLf
Dim strWordList : strWordList =3D oFS.OpenTextFile( s1FSpec ).Read=
All

WScript.Echo strWordList

Dim nPos, nLN
nPos =3D InStr( strWordList, strSearchWord )
If 0 < nPos Then
nLN =3D Len( Replace( Left( strWordList, nPos - 1 ), vbCrLf, "" ) )=

WScript.Echo "Line", (nPos - nLN) \ 2 + 1
Else
WScript.Echo "No " & strSearchWord
End If

----- output for my test file -----------

=3D=3D=3D findLN: find line number of string in file =3D=3D=3D=3D=3D
eins
zwei
drei
ST_NUM
f=FCnf
sechs
sieben

Line 4
=3D=3D=3D findLN: 0 done (00:00:00) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D

Re: Searching a text file for a line. by Paul

Paul
Fri Dec 07 17:36:29 PST 2007


<grout58@gmail.com> wrote in message
news:c3e22086-60af-490e-8c90-adf03776301a@s8g2000prg.googlegroups.com...
> Hey all, I'm trying to make a script that searches a text file for
> a
> line, takes the number on that line and sticks in in a variable. I
> have been working on it for about an hour with little success. I
> figure I need to use the readall method, but is there a way I can
> get
> the readline method to just grab the data off a certain line say
> like
> line 16 so I do not need to search the whole txt? Heres what I have
> so far.
>
> im filesys, text, readfile, contents, svrtag
> set filesys = CreateObject("Scripting.FileSystemObject")
> set readfile=filesys.OpenTextFile("C:\BRUNET\LIB\SET_BRUNET.CFG")
>
> strWordList = readfile.ReadAll
> readfile.Close
>
> strSearchWord = "ST_NUM" & vbCrLf
>
> If InStr(strWordList, strSearchWord) = True Then Wscript.echo
> strSearchWord
>
>
> Any help would be greatly appreciated.

I'm guessing that the OP knows which line the info is on (always line
16, for example), so perhaps the OP could use the skipline method to
quickly reach the line of interest. Something like:

For i = 1 to 15
readfile.SkipLine
Next
strWordList = readfile.ReadLine

Now the OP only has to extract the number of interest from that line.

-Paul Randall



Re: Searching a text file for a line. by James

James
Sun Dec 09 15:34:12 PST 2007

<grout58@gmail.com> wrote in message
news:c3e22086-60af-490e-8c90-adf03776301a@s8g2000prg.googlegroups.com...
> Hey all, I'm trying to make a script that searches a text file for a
> line, takes the number on that line and sticks in in a variable. I
> have been working on it for about an hour with little success. I
> figure I need to use the readall method, but is there a way I can get
> the readline method to just grab the data off a certain line say like
> line 16 so I do not need to search the whole txt? Heres what I have
> so far.
>
> im filesys, text, readfile, contents, svrtag
> set filesys = CreateObject("Scripting.FileSystemObject")
> set readfile=filesys.OpenTextFile("C:\BRUNET\LIB\SET_BRUNET.CFG")
>
> strWordList = readfile.ReadAll
> readfile.Close
>
> strSearchWord = "ST_NUM" & vbCrLf
>
> If InStr(strWordList, strSearchWord) = True Then Wscript.echo
> strSearchWord
>
>
> Any help would be greatly appreciated.

You could use the Split function to split the lines into an array and then
grab the specific line you wanted. To get the data from line 16 in your
file:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dim filesys, text, readfile, contents, svrtag
set filesys = CreateObject("Scripting.FileSystemObject")
set readfile=filesys.OpenTextFile("C:\BRUNET\LIB\SET_BRUNET.CFG")

strWordList = readfile.ReadAll
strLine = Split(strWordList, vbNewLine)(15)
readfile.Close
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The only thing I added to your code was "strLine = Split(strWordList,
vbNewLine)(15)"
Since arrays are zero based, you have to subtract one from the line number
you want.