Re: Replacing line in a text file?
Posted: January 7th, 2017, 3:52 pm
Well, I still don't see what line number has to do with anything, I gather though that you intend that if it is replacing "Internet" with "blah blah" you intend that it be restricted to a line that looks like:
<item> Internet </item>
in order to avoid unwanted replacements other places.
That can be done with something like this:
TextFile.txt:
LineReplace.ini:
LineReplace.lua:
<item> Internet </item>
in order to avoid unwanted replacements other places.
That can be done with something like this:
TextFile.txt:
Code: Select all
<grammar>
<one-of>
<rule>
<item> Games </item>
<item> Office </item>
<item> System </item>
<item> Internet </item>
<item> Graphics </item>
<item> Media </item>
</rule>
</one-of>
</grammar>
Code: Select all
[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
[MeasureReplace]
Measure=Script
ScriptFile=#CURRENTPATH#LineReplace.lua
FilePath=#@#TextFile.txt
UpdateDivider=-1
[MeterClicker]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Click Me
LeftMouseUpAction=[!CommandMeasure MeasureReplace """TextReplace("Internet", "Blah Blah")"""]
Code: Select all
function Initialize()
filePath = SELF:GetOption('FilePath')
end
function Update()
end
function TextReplace(oldText, newText)
filePath = SKIN:MakePathAbsolute(filePath)
local inFile = io.open(filePath, 'r')
if not inFile then
print('LineReplace: unable to open file at ' .. filePath)
return
end
inText = inFile:read('*a')
io.close(inFile)
outText = string.gsub(inText, '<item> '..oldText..' </item>', '<item> '..newText..' </item>')
local outFile = io.open(filePath, 'w')
outFile:write(outText)
io.close(outFile)
end