Need help with updating lines in a text file

Hello All,

I have a small, simple text file that I create, and read just fine.
Debug:False
Profiler:False
ShutDown:False
Restart:False

However, I now need to update a line with a new value by changing the boolean value to True (or False).

What is the best way to do this?? Sample code would really be helpful too!

Thanks,
Tim

It would be possible to read/write through a dictionary. I’ve written a sample, but it’s a really basic design that will show its weakness if the format strays from what you’ve described above.

I have a more robust INI in/out class, but that would be changing the format which you did not mention being an acceptable solution.

Dictionary Read / Write Example Project (2.5 KB)

^^ That project but as plain text code:
#tag Module
Protected Module modDict
	#tag Method, Flags = &h0
		Function FromString(sInput as String) As Dictionary
		  // Convert EOLs to known
		  sInput = sInput.ReplaceLineEndings(EndOfLine.UNIX)
		  
		  // Split on EOLs
		  var arsLines() as String = sInput.Split(EndOfLine.UNIX)
		  
		  // Parse the lines
		  var dict as new Dictionary
		  for each sLine as String in arsLines
		    var arsKeyValPair() as String = sLine.Split(":")
		    
		    // Not enough data
		    if arsKeyValPair.LastIndex < 1 then
		      System.DebugLog("Not enough data: " + sLine)
		      continue for sLine
		      
		    end
		    
		    dict.Value(arsKeyValPair(0)) = arsKeyValPair(1)
		    
		  next sLine
		  
		  return dict
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function ToString(extends dict as Dictionary) As String
		  var arsLines() as String
		  for each vKey as Variant in dict.Keys
		    arsLines.Add(vKey.StringValue + ":" + dict.Value(vKey).StringValue)
		    
		  next vKey
		  
		  // Intentionally using the EOL function because I know TimS is on Windows
		  return String.FromArray(arsLines, EndOfLine)
		End Function
	#tag EndMethod

End Module
#tag EndModule

it is just for your app or do you use it by something else, as example a batch file?
its better to read/write a JSONItem as config
https://documentation.xojo.com/api/text/json/jsonitem.html
or using a SQLite DB.

about your file basically you have a class which have a list(array) of Pair, and a method for load and save.
https://documentation.xojo.com/api/language/pair.html
https://documentation.xojo.com/api/language/dictionary.html

if you need to change the file content, it would read all, make a method that find and update Restart key, save the file. with the dictionary class you just assign the key a new value.

If your text file is really that straightforward and the format is well known, you could just replace the string like this:

Var sampleData As String
Var txtFile As FolderItem
Var ins As TextInputStream
Var outs As TextOutputStream

Var original, change As  String ' these values would be passed to this method
original = "Profiler:False"
change = "Profiler:True"
txtFile = FolderItem.ShowOpenFileDialog("text/plain") 

If txtFile <> Nil And txtFile.Exists Then
  ins = TextInputStream.Open(txtFile)
  sampleData = ins.ReadAll
  ins.Close
  
  sampleData = sampleData.ReplaceAll(original,change)
  
  outs = TextOutputStream.Create(txtFile)  ' Recreates the text file
  outs.WriteLine(sampleData)
  outs.Close
  
End If

Thanks everyone!
I appreciate your responses and help.
Tim