UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
2 Pages V < 1 2  (Go to first unread post)
   Reply to this topicStart new topic
> Delete Lines From A .csv File, Access 2013    
 
   
dhapp
post Dec 6 2018, 10:12 PM
Post#21



Posts: 1,134
Joined: 17-November 03
From: Hamburg, NY


After posting the new code I've made some modifications to make it more flexible.
Here's the first line:
CODE
Public Sub ReadTextFile(strPath As String, strFileName As String, iSkipLines As Integer)


--------------------
Doug
When the eagle is away, the crow says, "I am the eagle."
Go to the top of the page
 
WildBird
post Dec 6 2018, 10:23 PM
Post#22


UtterAccess VIP
Posts: 3,480
Joined: 19-August 03
From: Auckland, Little Australia


CODE
Public Sub ReadTextFile(ByVal strInputFileName As String, ByVal intSkip As Integer)
'   sample call:
'   ReadTextFile "C:\Users\Doug\Downloads\17.csv"

    Dim objFSOIn As Object
    Dim objFSOOut As Object
    Dim objTextIn As Object
    Dim objTextOut As Object
    'objTextOut
    Dim strTextLine As String

    Dim i As Integer
    
    
    Set objFSOIn = CreateObject("Scripting.FileSystemObject")
    Set objFSOOut = CreateObject("Scripting.FileSystemObject")
    Set objTextIn = objFSOIn.OpenTextFile(strInputFileName)
    Set objTextOut = objFSOOut.CreateTextFile("C:\Users\Doug\Downloads\NewFile.csv", True, True)
    
    Do While Not (objTextIn.AtEndOfStream)
        strTextLine = objTextIn.ReadLine
        strTextLine = Replace(strTextLine, ",", Chr(9))   'Replace each comma with Tab character
        i = i + 1
        If i > intSkip Then
            objTextOut.Write strTextLine & vbCRLF  'Write the line with a Carriage Return character.
        End If
    Loop
    
    objTextIn.Close
    objTextOut.Close
    Set objFSOIn = Nothing
    Set objFSOOut = Nothing
    Set objTextIn = Nothing
    Set objTextOut = Nothing

End Sub


Added a parameter to allow number of lines to skip to be passed. OP said it could be different numbers of lines to skip.

Also changed the Chr(13) with VBCRLF - cant remember why, but there was some issues I can recall when only using one, not a carriage return and line feed. I think....!

--------------------
Beer, natures brain defragging tool.
Go to the top of the page
 
WildBird
post Dec 6 2018, 10:24 PM
Post#23


UtterAccess VIP
Posts: 3,480
Joined: 19-August 03
From: Auckland, Little Australia


I was slow on posting above post!

Agree, should have path, file name, and number of lines to skip.


--------------------
Beer, natures brain defragging tool.
Go to the top of the page
 
MadPiet
post Dec 6 2018, 10:56 PM
Post#24



Posts: 2,599
Joined: 27-February 09



How will you know how many junk lines there are in a file? Is is "all the lines up to and including the first blank line" that are junk? If that's the case, then why test the strLine variable's length each time until you hit one that's blank, and then start writing to the output file? (Well, if tina's solution doesn't work...)

CODE
    Dim blnStartWriting As Boolean = False  '-- Don't Write anything to the output file until this is true
    
    Do While Not (objTextIn.AtEndOfStream)
                strTextLine = objTextIn.ReadLine
        blnStartWriting = (Len(Trim(strTextLine)=0)   '-- if the string contains something, start writing the lines to output file.
        
                if blnStartWriting Then
                    strTextLine = Replace(strTextLine, ",", Chr(9))   'Replace each comma with Tab character
                    objTextOut.Write strTextLine & vbCRLF  'Write the line with a Carriage Return character.
               End If
       Loop
Go to the top of the page
 
2 Pages V < 1 2


Custom Search


RSSSearch   Top   Lo-Fi    12th December 2018 - 06:52 PM