Solved

Inserting text into text file

Posted on 2001-06-25
8
179 Views
Last Modified: 2010-05-18
Hello.  I am trying to insert text into my .ini file.  here is my ini file

#
#
#My ini file
#
StartOfUsersMail
soso@nasa.gov
who@nasa.gov
here@nasa.gov
#
#
#

Ok...How do I insert text into my ini between soso@nasa.gov and who@nasa.gov    I don't want put it at the end of the file.  Do I have to write a function to go out and totally rewrite the ini file over???
Thanks
 
     
0
Comment
Question by:airbronedude
8 Comments
 
LVL 4

Expert Comment

by:wileecoy
ID: 6225237
How does your program track where you want it to be placed?
0
 
LVL 20

Expert Comment

by:hes
ID: 6225293
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6225299
Sorry it's brief, but you need to use the SetPrivateProfileString and GetPrivateProfileString

Good Luck

Simon
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6225402
Yes, you need to rewrite whole file:

Private Sub Command1_Click()
Dim fso As New FileSystemObject, s As String, n As Long
Dim streamS As TextStream, searchS As String, insertS As String
Dim s1 As String, s2 As String, s3 As String
searchS = "soso@nasa.gov"
insertS = "somethign@nasa.gov"
Set streamS = fso.OpenTextFile("c:\test\test.ini")
s = streamS.ReadAll
n = InStr(s, searchS)
s1 = Mid(s, 1, n + Len(searchS) - 1)
s2 = vbCrLf & insertS
s3 = Mid(s, n + Len(searchS))
streamS.Close
Set streamS = fso.OpenTextFile("c:\test\test.ini", ForWriting)
streamS.Write s1 & s2 & s3
End Sub
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
jklmn earned 50 total points
ID: 6225410
Ooops, you need to close the file:
streamS.Close
after
streamS.Write s1 & s2 & s3
and before End sub.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6225541
"Yes, you need to rewrite whole file"

Either that or someone else does, such as Windows and the previously mentioned SetPrivateProfileString.

The way textfiles are stored on disk, they are written in consecutive bytes until a sector fills up.  If you want to insert something, there's three ways for the O/S to handle it:

1) Shift all of the bytes forward to make space, then insert
2) Re-write all of the bytes to a new place, then delete the old
3) Replace some characters with a marker that points to the location of the inserted text

#1 is a convenient approach, but is dangerous because if the electricity goes out while writing, your new file isn't done writing, and the original one is now corrupt
#2 is easy to do, but is usually the slowest for writing; with high-speed drives, this is usually the approach of modern O/Ss
#3 is the fasted for writing, but the slowest for reading.  I think MSWord uses a variation of this.

--
So you have the choice of simulating the O/S, or letting the O/S do the work for you!  Your choice.  Pick either jklmn or simonbennett (or maybe hes' link helped.)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139803
Hi airbronedude,
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 suggest to:

    Accept jklmn's comment(s) as an answer.

airbronedude, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

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

Expert Comment

by:Computer101
ID: 7178887
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use closed file on desktop in vba 6 65
Put text in a picture ASP.NET C# 2 50
Macro Excel - Multiple If conditions 2 64
VB6 ListBox Question 4 32
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

911 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

21 Experts available now in Live!

Get 1:1 Help Now