1

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

sir can i ask what will i do on the line that has commented? just a sample on replacing of string. thanks
Check the documentation for the Replace function over here. msdn.microsoft.com/nl-nl/library/bt3szac5(v=vs.80).aspx
Affirmative, something like that should work: TempS=Replace(TempS, "Alarm2", "AlarmChanged")
Thanks! I found it very useful. But I implemented it using: TempS = TS.ReadAll Final = Replace(TempS, "Alarm2", "Alarm") because it is much faster in large files
0
  • 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.