Changing the last line of a CSV text file

J

Jac Tremblay

Hi, all of you
I have over 20 text files, some of which may include over 40000 lines of text. I only need to remove the return of the last line. Here is some code that works, thanks to kkknie
' *********************************************************************
Sub TestWithOpen(
' Jac Tremblay 2004-04-1
Dim strPathDat As Strin
Dim strNameDat As Strin
Dim strNameDat2 As Strin

Dim strLineText As Strin
Dim intI As Intege
Dim intJ As Intege
Dim strLine() As Strin

strPathDat = "C:\DaDest\
strNameDat = "SCAT0107.DAT
strNameDat2 = "SCAT0199.DAT

Open strPathDat & strNameDat For Input As #
Open strPathDat & strNameDat2 For Output As #
intI =

Do While Not EOF(1
Line Input #1, strLineTex
intI = intI +
ReDim Preserve strLine(intI
strLine(intI) = strLineTex
Loo

For intJ = 1 To int
strLineText = strLine(intJ
If intJ = intI The
Print #2, strLineText
Els
Print #2, strLineTex
End I
Next int

Close #
Close #
End Su
' *********************************************************************
Now, I think that this code could be simpler but cannot figure out how. For one thing, I know that I will have to to use Long instead of Integer for the big files. But the main problem is that I need to store all the lines in a temporary array an then rewrite the whole file in a second file. I will later have to delete the fisrt one. What a waste of energy and precious time!..
Anyone can help

Thank you.
 
J

Jac Tremblay

Hi again
Is there a way to modify the last line of a text file without rewriting the whole file and/or storing each line in a temporary string array. When doing so, one has to delete the old file and rename the new one to the old name. Here is some code that works fine but I would like to use a type of "Open File.csv For Random" or something like this. I would also need to have a way of knowing which line is the last one

Does anyone have an idea
' *********************************************************************
Option Explici

Private Declare Function MoveFile Lib "kernel32.dll"
Alias "MoveFileA" (ByVal lpExistingFileName As String,
ByVal lpNewFileName As String) As Lon

' *********************************************************************
Sub TestWithOpen(
' Jac Tremblay 2004-04-1
Dim strPathDat As Strin
Dim strNameDat As Strin
Dim strNameDat2 As Strin

Dim strLineText As Strin
Dim intI As Intege
Dim intJ As Intege
Dim strLine() As Strin
Dim strResponse As Strin

strPathDat = "C:\DaDest\
strNameDat = "SCAT0204.DAT
strNameDat2 = "SCAT0199.DAT

Open strPathDat & strNameDat For Input As #
Open strPathDat & strNameDat2 For Output As #
intI =

Do While Not EOF(1
Line Input #1, strLineTex
intI = intI +
ReDim Preserve strLine(intI
strLine(intI) = strLineTex
Loo

For intJ = 1 To int
strLineText = strLine(intJ
If intJ = intI The
Print #2, strLineText
Els
Print #2, strLineTex
End I
Next int
Close #
Close #
Kill strPathDat & strNameDa
strResponse = MoveFile(strPathDat & strNameDat2, strPathDat & strNameDat
End Su
' *********************************************************************
Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top