?
Solved

reading and writing to a text file

Posted on 2002-04-09
16
Medium Priority
?
217 Views
Last Modified: 2010-05-02
Okay I know how to create a txt file, now I want to write to it....
I have a txt file created. Now I want to write to it, first thing I want to do is parse it and validate that this string does not exists in the file.
The format I want written to this file is.

name1
name2
name3

Meaning I want to check the file to see if name4 exists, if it doesn't the add it to the bottom, if it does exist, then ignore.I want the pattern kept the same, I know we need to incorporate the special character for a carriage return.
Any ideas? Need help asap.
0
Comment
Question by:chicabow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +4
16 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 800 total points
ID: 6928951
Private Sub Command1_Click()

    insertNoDup ("name3")
    insertNoDup ("name4")

End Sub

Public Sub insertNoDup(entry As String)

Dim fileNum As Integer
Dim curRec As String

fileNum = FreeFile

Open "C:\my documents\test.txt" For Input As #fileNum

Do While Not EOF(fileNum)
    Line Input #fileNum, curRec
    If curRec = entry Then '' name found exit
        Close (fileNum)
        Exit Sub
    End If
Loop

'' if we get here, not found, add name to end of list
Close (fileNum)
fileNum = FreeFile
Open "C:\my documents\test.txt" For Append As #fileNum
Print #fileNum, entry
Close (fileNum)

End Sub

0
 
LVL 4

Expert Comment

by:Glowman
ID: 6928957
well you could use the FSO to read each line to see if your name already exists.  The syntax would be :

Dim Line as string
Dim FileTool As New Scripting.FileSystemObject
Dim File As Scripting.TextStream
Dim Name1 as string

Name1="Jimbo"

set File=FileTool.OpenTextFile(App.path & "\MyFile",True,True)

do until File.AtEndOfStream

line=File.ReadLine

If trim(Line)=Name1 Then 'This could take many conditions
  Exit Do
else
  File.writeline Name1
End if

loop


That is a basic example I could give you some more if needed.  Oh ya you will need to reference the DLL :
"Microsoft Scripting Runtime" to use the FSO
Hope it helps
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6928959
Private Sub Form_Load()
Dim ff As Integer
Dim sContents As String
ff = FreeFile


Open "c:\myfile.txt" For Input As #ff
   sContents = Input(LOF(ff), 1)
Close #ff

If Not (InStr(1, Right$(sContents, 10), "name4", vbTextCompare)) Then
    Open "c:\myfile.txt" For Output As #ff
        Print #ff, sContents & vbCrLf & "name4"
    Close #ff
End If
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:bobbit31
ID: 6928965
Glowman, the code you provided will write a line after every non-matching entry in the file
0
 
LVL 4

Expert Comment

by:Glowman
ID: 6928976
Bobbit your right.  chicabow I am sorry if this confused you.  The logic needs a little work but the concept is there.  Respond if you want more.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6929012
Hey, i am here too!
;)
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6929126
Observation, chicabow:

What you are describing is a database function.
What you have is a text-based database without any of the functionality of a database.
To succeed, you need to build your own set of database tools.

My recommendation (assuming that this will grow larger) is to move it to a real* database now (such as ADO or DAO.)  However, there is increased overhead, so that may be a consideration.

* Although some may argue that the Access engine is not a "real" database, it performs most of the functions of a real database, especially for a project such as the one described.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6929235
rspahitz, i agree with you but chicabow never saids that would be a "name5" item.
0
 
LVL 1

Author Comment

by:chicabow
ID: 6929362
bobbit31 and Glowman and Richie_Simonetti  all posted now what do I do?
Which is more efficient?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6929532
well richies, reads the whole file into memory where mine does it line by line.

Glowman's uses FSO, and while it will work, FSO has more overhead than the native file i/o methods used by myself and richie.

Theorhetically, if your flat file is going to be very large, i agree with rspahitz to go w/ a database.  If you don't want to do this i believe both mine and richies solution will run in about the same amount of time. (it depends on the location of the search string, if found)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6929620
Bobbit31, i think you did a good explanation.
chicabow, if you have time, try each one and see whitch best meets your requirements.
Cheers

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6929656
well, don't read entire file in memory:
Private Sub Form_Load()
Dim ff As Integer, flen As Long
Dim sContents As String
Dim arrb() As Byte
ff = FreeFile
ReDim arrb(10)
flen = FileLen("c:\myfile.txt") - 10
Open "c:\myfile.txt" For Binary As #ff
  Get #ff, flen, arrb
Close #ff
sContents = StrConv(arrb, vbUnicode)
If Not (InStr(1, Right$(sContents, 10), "name4", vbTextCompare)) Then
   Open "c:\myfile.txt" For Output As #ff
       Print #ff, sContents & vbCrLf & "name4"
   Close #ff
End If
End Sub

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6929661
ups!
sorry...
....
....
...
If Not (InStr(1, Right$(sContents, 10), "name4", vbTextCompare)) Then
   Open "c:\myfile.txt" For Append As #ff
       Print #ff, vbCrLf & "name4"
   Close #ff
End If
End Sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6929672
sorry again...


If InStr(1, sContents, "name4", vbTextCompare) <= 0 Then
   Open "c:\myfile.txt" For Append As #ff
       Print #ff, "name4"
   Close #ff
End If
End Sub
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7643135
Hi chicabow,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept bobbit31's comment(s) as an answer.
    *** an excellent simple solution to a simple problem.

chicabow, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:kodiakbear
ID: 7673352
Moving to the PAQ

kb
Experts Exchange Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question