-
Sep 13th, 2013, 11:09 AM
#1
Thread Starter
Lively Member
[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
-
Sep 13th, 2013, 12:33 PM
#2
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)
-
Sep 13th, 2013, 10:57 PM
#3
Thread Starter
Lively Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|