Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vbscript code pulls only the last line of a file rather than the whole file

Status
Not open for further replies.

kdjonesmtb2

Technical User
Nov 19, 2012
93
US
The vbscript code below pulls the last line of an existing txt file and adds it to new file - I wanted the code to pull all the lines in the exisitng txt file starting from row 2

Const ForReading = 1, ForWriting = 2, ForAppending = 8


Set fso = CreateObject("Scripting.FileSystemObject")
Set f99 = fso_OpenTextFile("\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest1.x12", ForReading,True)

'Skip through the first two lines
f99.readline
f99.readline

'Read till the end of file, if the line is not "end", split it based on ","
while not f99.AtEndOfStream
line = f99.readline()
if line <> "end" then
arr=split(line, "~")
q1.close
' Set fso=createobject("Scripting.FileSystemObject")
Set qfile1 = fso.CreateTextFile("\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest_full.x12", ForAppending,True)
for each x99 in arr
qfile1.write(x99 & vbcrlf )


next



end if
wend


f99.close


'qfile1.Close

objworkbook.close 'Closes the Excel workbook each time a test case is incremented in the Global DataTable
 
Hi [peace]
Try to implement SkipLine Method

Code:
Function SkipLineInFile
   Const ForReading = 1, ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
   f.Write "Hello world!" & vbCrLf &"Hello world!" & vbCrLf &"Hello world!" & vbCrLf & "VBScript is fun!"
   Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
   For i=1 To 3
   f.SkipLine
   Next
   SkipLineInFile = f.ReadAll
End Function
MsgBox SkipLineInFile

 
Hello,

Thanks the code works great I made some modifications that work for my scenario:

If I want to also remove the last 2 lines in the source files - what is the syntax for that?


If row=1 Then


Function NoSkipLineInFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f99, x99
Set fso = CreateObject("Scripting.FileSystemObject")

Set f99 = fso_OpenTextFile("\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest_full.x12", ForAppending,True)
' f99.Write "Hello world!" & vbCrLf &"Hello world!" & vbCrLf &"Hello world!" & vbCrLf & "VBScript is fun!"
'99.Write(SkiplineInFile)
Set x99 = fso_OpenTextFile("\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest" & cstr(row)& ".x12", ForReading)
' For i=1 To 3
' x99.SkipLine
' Next
NoSkipLineInFile = x99.ReadAll
f99.Write(NoSkipLineInFile)
End Function
MsgBox NoSkipLineInFile

End if



If row>=2 Then


Function SkipLineInFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f99, x99
Set fso = CreateObject("Scripting.FileSystemObject")

Set f99 = fso_OpenTextFile("\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest_full.x12", ForAppending,True)
' f99.Write "Hello world!" & vbCrLf &"Hello world!" & vbCrLf &"Hello world!" & vbCrLf & "VBScript is fun!"
'99.Write(SkiplineInFile)
Set x99 = fso_OpenTextFile("\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest" & cstr(row)& ".x12", ForReading)
For i=1 To Full_File_Skipline_value
x99.SkipLine
Next
SkipLineInFile = x99.ReadAll
f99.Write(SkipLineInFile)
End Function
MsgBox SkipLineInFile

End if



objworkbook.close 'Closes the Excel workbook each time a test case is incremented in the Global DataTable



 
Hi [peace]
Try to implement someting like this :

Code:
Option Explicit
Dim ws,Title,LogFile
Title = "SkipLine Method"
Set ws = CreateObject("Wscript.Shell")
WriteLog(SkipLineInFile(2,2))
MsgBox SkipLineInFile(2,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
   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)
   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 & vbCrLf & MyArray(j)
   Next
   SkipLineInFile = MyText
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
 
Hi [peace]
I share with you another solution with this function ExtractLinesFromTextFile

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 "erreur de depassement de lignes", vbCritical, "erreur de depassement de lignes"
    End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top