• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

reading and writing to a text file

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
chicabow
Asked:
chicabow
  • 7
  • 3
  • 2
  • +4
1 Solution
 
bobbit31Commented:
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
 
GlowmanCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
bobbit31Commented:
Glowman, the code you provided will write a line after every non-matching entry in the file
0
 
GlowmanCommented:
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
 
Richie_SimonettiIT OperationsCommented:
Hey, i am here too!
;)
0
 
rspahitzCommented:
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
 
Richie_SimonettiIT OperationsCommented:
rspahitz, i agree with you but chicabow never saids that would be a "name5" item.
0
 
chicabowAuthor Commented:
bobbit31 and Glowman and Richie_Simonetti  all posted now what do I do?
Which is more efficient?
0
 
bobbit31Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
DanRollinsCommented:
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
 
kodiakbearCommented:
Moving to the PAQ

kb
Experts Exchange Moderator
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now