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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing Text files in VB 3

Status
Not open for further replies.

msturges

Programmer
Mar 29, 2001
32
ZA
Hi All,

This may come across as a rather amateur question but....

If I wanted to read in a normal text file, use the data and then delete the "current record" within the file, how would I go about doing this? At the moment this is what I am trying :

Open "c:\temp\EDI20010521.txt" For Random As #1

While Not EOF(1)
Input #1, test2
FromANA = Mid(test2, 21, 35)
FileSize = Mid(test2, 143, 8)
If FromANA = EDINum Then
totalFileSize = totalFileSize + Val(FileSize)
Delete test2
End If
Wend

However, when I run the app I get an error : "Sub or Function not defined." and the word Delete within my code is highlighted.

I'm flying blind here 'cos i'm not even sure if "Delete" is a valid command, and I can't seem to find much assitance in the reference guide.
 
You are correct "delete" is not a valid command - answer: open the input file (as you have done) and write out another file deleting/adding whatever you wish and then at the end of the file, close it then delete the original file (or rename it) and then rename the new file to the same name as the original file.

Enjoy - please post if you need further details: happy to assist.
 
There is no delete command in VB . . . the proper command to use is Kill (I.e. Kill C:\MyFile.txt). - Jeff Marler B-)
 
MSturges,

If I understand you right, you are reading in individual records from a text file and comparing them to your condition. If the record matches the condition, you want it deleted from the file and all records that don't match you want to remain intact. If that is correct, the following code should work.

Code:
Open "c:\temp\EDI20010521.txt" For Input As #1
Open "c:\temp\temp.txt" For Output As #2

Do While Not EOF(1)
    Input #1, test2
    FromANA = Mid(test2, 21, 35)
    FileSize = Mid(test2, 143, 8)
    If FromANA = EDINum Then
        totalFileSize = totalFileSize + Val(FileSize)
    Else
        Output #2, test2
    End If
Loop

Close #1
Close #2

Open "c:\temp\temp.txt" For Input As #1
Open "c:\temp\EDI20010521.txt" For Output As #2

Do While Not EOF(1)
    Input #1, test2
    Output #2, test2
Loop

Close #1
Close #2
Kill ("c:\temp\temp.txt")

Hope this helps.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top