Is there a way to read a certain line of a text file using vbscript and storing it as a variable. Thanks.
Asked
Active
Viewed 2,132 times
0
-
1See http://stackoverflow.com/questions/15533214/how-do-i-read-a-file-line-by-line-in-vb-script – Phylogenesis Oct 15 '15 at 16:06
-
1http://blogs.technet.com/b/heyscriptingguy/archive/2005/01/25/how-can-i-read-just-a-single-specified-line-from-a-text-file.aspx – JNevill Oct 15 '15 at 16:27
-
Thank you JNevill, and in response to you John Coleman I have spent about an hour searching for an answer to no avail and that is why I am posting my question here so that maybe some kind hearted people may make my life a bit easier and stop me from spending hours on end searching the web. – Oct 15 '15 at 16:40
-
@JosephBunce If you actually read either link that was provided by JNevill or Phylogenesis then you'll see that they answer your question. – Lews Therin Oct 15 '15 at 17:35
-
@Sylverac I have read them and they do, that is why I was thanking JNevill – Oct 16 '15 at 17:25
1 Answers
2
Try this example :
Option Explicit
Dim ws,MyFile
Set ws = CreateObject("WScript.Shell")
MyFile = "%Windir%\system32\slmgr.vbs"
MyFile = ws.ExpandEnvironmentStrings(MyFile)
wscript.echo ExtractLinesFromTextFile(Myfile,1,2) 'Extract line from line 1 to 2
wscript.echo ExtractLinesFromTextFile(Myfile,2,2) 'Extract line N°2
wscript.echo ExtractLinesFromTextFile(Myfile,1,5) 'Extract line from line 1 to 5
wscript.echo ExtractLinesFromTextFile(Myfile,1,10)'Extract line from line 1 to 10
wscript.echo ExtractLinesFromTextFile(Myfile,8,20)'Extract line from line 8 to 20
wscript.echo ExtractLinesFromTextFile(Myfile,8,40)'Extract line from line 8 to 40
wscript.echo ExtractLinesFromTextFile(Myfile,8,50)'Extract line from line 8 to 50
'*********************************************************************************************************
Public Function ExtractLinesFromTextFile(ByRef TextFile, ByRef FromLine, ByRef ToLine) '<-- Inclusive
If FromLine <= ToLine Then
With CreateObject("Scripting.FileSystemObject").OpenTextFile(TextFile)
Do Until .Line = FromLine Or .AtEndOfStream
.SkipLine
Loop
Do Until .Line > ToLine Or .AtEndOfStream
ExtractLinesFromTextFile = ExtractLinesFromTextFile & (.ReadLine & vbNewLine)
Loop
End With
Else
MsgBox "erreur de depassement de lignes", vbCritical, "erreur de depassement de lignes"
End If
End Function
'*********************************************************************************************************

Hackoo
- 18,337
- 3
- 40
- 70