CeciliA
CeciliA

Reputation: 45

How to Replace Strings in a Text File VB6

i have a problem right now. Im using textfile as my database. Here is the format of my Strings inside the textfile.

01/Alarm1/01:00:00

01/Alarm2/02:00:00

01/Alarm3/03:00:00

For example i just want to rename the Alarm2 but it will keep the 01 on the beginning and the time on it. Thanks for help guys

Upvotes: 1

Views: 5864

Answers (2)

arif
arif

Reputation: 55

  • open txt file;

loop begin

  • get the string(by rows) - lets consider it is 'someStr'
  • you can use NewStr = Replace(someStr,"Alarm","Warning")
  • write NewStr to a new file

loop end

close both files

Upvotes: 0

Jeandey Boris
Jeandey Boris

Reputation: 743

Please find an example on which you can rely on in order to:

  1. read a text file
  2. update its content
  3. write it back

Hope this will help you.

Dim FSO As FileSystemObject
Dim TS As TextStream
Dim TempS As String
Dim Final As String
Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile("mydata.txt", ForReading)
Do Until TS.AtEndOfStream
    TempS = TS.ReadLine
    'if TempS contains Alarm2 => rename Alarm
    Final = Final & TempS & vbCrLf
Loop
TS.Close

Set TS = FSO.OpenTextFile("mydata.txt", ForWriting, True)
    TS.Write Final
TS.Close
Set TS = Nothing
Set FSO = Nothing

Upvotes: 2

Related Questions