Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

[RESOLVED] How to extract part of a text between line (n) and line (m) in a text file ?

$
0
0
Hi :wave:
I'm writing this Function SkipLineInFile(n,m) to extract part of a text between line (n) and line (m) in a text file.
So it just remains for me an error handling for overflow lines.
How can i do that ?
Any solution ?
Thank you in advance ;)
Code:

Option Explicit
Dim ws,Title,LogFile
Title = "SkipLine Method"
Set ws = CreateObject("Wscript.Shell")
WriteLog(SkipLineInFile(10,2))
MsgBox SkipLineInFile(10,2),64,Title
LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "log"
ws.run LogFile
'***************************************************************************************
Function SkipLineInFile(n,m)
  Const ForReading = 1, ForWriting = 2
  Dim fso,f,i,j,LireTout,MyArray,MyText,x,y
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
  f.Write "1ere Line" & vbCrLf &"2eme Line" & vbCrLf &"VBScript" & vbCrLf & "est Cool !"& vbCrLf &"6 Line" & vbCrLf &"7 Line"
  Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
  y=f.ReadAll
  x= f.Line
  msgbox x
  If n > y Then
  MsgBox "erreure de depassement de lignes",16,"erreure de depassement de lignes"
  wscript.quit
  else
  For i = 1 To n
      f.SkipLine
  Next
  Mytext = ""
  LireTout = f.ReadAll
  MyArray = Split(LireTout,vbCrLf)
  For j = LBound(MyArray) To Ubound(MyArray) - m
      MyText = Mytext & MyArray(j) & vbCrLf
  Next
  SkipLineInFile = MyText
  End if
End Function

Sub WriteLog(strText)
Dim fs,ts,LogFile
Const ForAppending = 8
    LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "log"
    Set fs = CreateObject("Scripting.FileSystemObject")
    if fs.FileExists(LogFile) Then
        fs.DeleteFile LogFile
    end if
    Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
    ts.WriteLine strText
    ts.Close
End Sub


Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>