Solved

reading and writing to a text file

Posted on 2002-04-09
16
208 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
  • 7
  • 3
  • 2
  • +4
16 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now