Results 1 to 3 of 3

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

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Location
    http://bbat.forumeiro.com/
    Posts
    86

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

    Hi
    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


  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to extract part of a text between line (n) and line (m) in a text file ?

    Try:

    Code:
    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 "erreure de depassement de lignes", vbCritical, "erreure de depassement de lignes"
        End If
    End Function
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Location
    http://bbat.forumeiro.com/
    Posts
    86

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

    Hi ! Bonnie West
    I want to thank you for this clever solution that you post this is what i need to do
    So the Problem is solved thanks to you !


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width