It is currently September 20th, 2019, 9:15 pm

Replacing line in a text file?

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

jsmorley » 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:

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>
LineReplace.ini:

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")"""]
LineReplace.lua:

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
LineReplace_1.0.rmskin
You do not have the required permissions to view the files attached to this post.
User avatar
StArL0rd84
Posts: 336
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Replacing line in a text file?

StArL0rd84 » January 7th, 2017, 4:06 pm

Wow thank you.
have run out of time to play with this right now though.
ill try it out later tonight and let you know.
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

jsmorley » January 7th, 2017, 4:08 pm

Probably the only thing about this that makes me a hair nervous is that if the "write" action fails for some reason, and I'm not sure what could possibly cause that, but if your system crashed in the middle of the write for instance, the .inc file containing your information could be corrupted or even lost. I'd be temped to create a ".bak" copy of the file in Lua first, and ensure that succeeded, before opening the original for "write".

There are tons of ways you might make this disaster-proof, you might double check that you successfully read the file, by looking for </grammar> in the result or something, you might only write the output file if the string value of "inText" and "outText" are different, and outText contains </grammar>, probably others. Not sure you need to get too anal about it though, this is a straightforward and REALLY fast operation that is unlikely to fail.
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

jsmorley » January 7th, 2017, 4:41 pm

One other thing to keep in mind with this, is that the text file you are reading / writing must be UTF-8 w/o BOM Unicode or ANSI encoding. Lua just can't properly deal with UTF-16 Unicode files.