Remove lines from textfile

bodylojohn

Registered User.
Local time
Today, 14:27
Joined
Dec 28, 2005
Messages
205
Hello,

I want to import a .txt file into my access application.
However there are 6 lines of comment I wish to delete from the textfile.

I have a button on a form.
when I press the button the 6 lines should be deleted.

Does anybody have an idea how to go about this?
 
import the textfile and delete the records in the table. That's much easier than removing the lines from a textfile.
 
Alot depends on where these 6 lines of text appear

a) at the top
b) at the bottom
c) in the middle
d) spaced out

Will there be consistancy
 
import the textfile and delete the records in the table. That's much easier than removing the lines from a textfile.

Then is there a way to delete the first 6 rows in the table automatically?
Without any interaction of the user.
 
Code:
Dim ff As Long
Dim TmpFile As String
Dim nLines As Long
Dim TempStr As String

ff = FreeFile
TmpFile = "C:\Temp.Txt"



Open [B]YourFileName [/B]For Input As #1
Open TmpFile For Output As #2

  Do While Not EOF(1)  ' check if at end of file
    nLines = nLines  + 1
    If nLines > 6 Then
        Input #1, TempStr
        Print #2, TempStr
    End If
  Loop

  Close #1
  Close #2

The above code opens the incoming text file and reads it line by line. When it gets to line 7 (first row with actual data) it writes it to a temp file. At the end it closes both files. You can now use the temp file to import into Access.

It will be a clone of the first file without the first 6 lines of text.

David
 
Thanks for the quick response guys.

I tried using the following code:
Code:
Private Sub cmdTest_Click()
Dim ff As Integer
Dim TmpFile As String
Dim nLines As Integer
Dim TempStr As String

ff = FreeFile
TmpFile = "z:\ProgjeJeroen\Temp.txt"

Open "Z:\ProgjeJeroen\uitslagen_kopie.txt" For Input As #1
Open TmpFile For Output As #2

  Do While Not EOF(1)  ' check if at end of file
    nLines = nLines + 1
    If nLines > 6 Then
        Input #1, TempStr
        Print #2, TempStr
    End If
  Loop

  Close #1
  Close #2
End Sub

But it doesnt delete the first 6 rows in the txt file.

But it creates another text file where the lines are cut off at the half and put on the row below.

What am i doing wrong???
 
Code:
Dim ff As Long
Dim TmpFile As String
Dim nLines As Long
Dim TempStr As String

ff = FreeFile
TmpFile = "C:\Temp.Txt"



Open [B]YourFileName [/B]For Input As #1
Open TmpFile For Output As #2

  Do While Not EOF(1)  ' check if at end of file
    nLines = nLines  + 1
    If nLines > 6 Then
        Input #1, TempStr
        Print #2, TempStr
    End If
  Loop

  Close #1
  Close #2

The above code opens the incoming text file and reads it line by line. When it gets to line 7 (first row with actual data) it writes it to a temp file. At the end it closes both files. You can now use the temp file to import into Access.

It will be a clone of the first file without the first 6 lines of text.

David

David..

Thanks again for the reply.
I also have another question about your code:

What does the "ff" variable do?
What does the "TempStr" variable do?
 
The ff is a variable that the next available number of a freefile. most people don't bother with this and simply give it a number, such as #1 or #2.

The TempStr is the line of text that the Input #1 is reading in this is passed to the variable which in turn is writing it to the new file.

If you send me the top ten lines or so from a sample file I will run it through and test it for you. Early indications are that you have line teminators in your string. Such as Chr(10) or carriage returns. Anyway I will be able to test that after.

David
 
Hello,

Here is the textfle i am trying to remove the first 5 lines.
I hope you can find it.

thanks in advance.
 

Attachments

Hi John

Have tested you file and below is the code you need for it to work correctly

Code:
Function ImportTextFile(StrFileName As String, StrTempFile As String, StartLine As Integer, Headings As Boolean)
'*************************************************************************************************************************
'Created by :D Crake Icr@fT Limited
'Date       :20th November 2008
'Arguments  :StrFileFile - Name of file to read
'           :StrTempFile - Name of file to create
'           :StartLine   - Which line contains the first row of data
'           :Headings    - Include the headings as row one in the output file
'
'Purpose    :To open a given text file and remove the n number of rows that contain data that does not need to be imported
'           :and save the actual data, including headings, if apporpriate, to a new text file.
'           :The newly created text file can then be used to import the raw data from.
'Headings   :If the file is being imported into a new table then set the headings argument to true
'           :If appending to an exisiting table then set the headings argument to false.
'**************************************************************************************************************************
'Copywrite  :There is no copywrite on this code, however, if passed on then pass on the above comments.
'**************************************************************************************************************************

'Temp variables
Dim nLines          As Long     'line count
Dim TempStr         As String   'Incoming line string

Open StrFileName For Input As #1
Open StrTempFile For Output As #2

'Do you need the headings in the output file
If Headings = True Then
    StartLine = StartLine - 1
End If

  Do While Not EOF(1)  ' check if at end of file
    nLines = nLines + 1
        Line Input #1, TempStr 'Read incoming text
        If nLines > StartLine Then
            Print #2, TempStr 'write outgoing text
        End If
  Loop
'close both files
  Close #1
  Close #2
  
End Function

Can I make a suggestion though about your source data.

Firstly if the numeric data is going into numeric fields you will need to do a replace on the - (hash) symbols. Again the comma between the date and time would need to be removed if you are copying this into a date/time field.

To do this insert the lines

StrTemp = Replace(StrTemp,"-","")
StrTemp = Replace(StrTemp",","")

between the Line Input command and the If nLines command

Hope this helps

David
 
Hi John

Have tested you file and below is the code you need for it to work correctly

Code:
Function ImportTextFile(StrFileName As String, StrTempFile As String, StartLine As Integer, Headings As Boolean)
'*************************************************************************************************************************
'Created by :D Crake Icr@fT Limited
'Date       :20th November 2008
'Arguments  :StrFileFile - Name of file to read
'           :StrTempFile - Name of file to create
'           :StartLine   - Which line contains the first row of data
'           :Headings    - Include the headings as row one in the output file
'
'Purpose    :To open a given text file and remove the n number of rows that contain data that does not need to be imported
'           :and save the actual data, including headings, if apporpriate, to a new text file.
'           :The newly created text file can then be used to import the raw data from.
'Headings   :If the file is being imported into a new table then set the headings argument to true
'           :If appending to an exisiting table then set the headings argument to false.
'**************************************************************************************************************************
'Copywrite  :There is no copywrite on this code, however, if passed on then pass on the above comments.
'**************************************************************************************************************************

'Temp variables
Dim nLines          As Long     'line count
Dim TempStr         As String   'Incoming line string

Open StrFileName For Input As #1
Open StrTempFile For Output As #2

'Do you need the headings in the output file
If Headings = True Then
    StartLine = StartLine - 1
End If

  Do While Not EOF(1)  ' check if at end of file
    nLines = nLines + 1
        Line Input #1, TempStr 'Read incoming text
        If nLines > StartLine Then
            Print #2, TempStr 'write outgoing text
        End If
  Loop
'close both files
  Close #1
  Close #2
  
End Function

Can I make a suggestion though about your source data.

Firstly if the numeric data is going into numeric fields you will need to do a replace on the - (hash) symbols. Again the comma between the date and time would need to be removed if you are copying this into a date/time field.

To do this insert the lines



between the Line Input command and the If nLines command

Hope this helps

David

Bro,

You are a lifesaver!!!

Thank you very much it works perfect.

Thank You Thank you thank you!!!!!
 
Glad to see that we have another satisfied customer.:)
 

Users who are viewing this thread

Back
Top Bottom