Solved

reading and writing to a text file

Posted on 2002-04-09
16
215 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

695 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