It is currently April 19th, 2024, 9:15 am

Delete from a file ?

Discuss the use of Lua in Script measures.
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Delete from a file ?

Post by AlC »

Hey,

Summary:

In this topic I wrote lines to a file. And now I want to delete some of the lines again.

This is how notes.hol looks at the moment:
<Event Month="7" Day="10" Year="2012">-geilomatiko</Event>
<Event Month="7" Day="10" Year="2012">-come on</Event>
<Event Month="9" Day="11" Year="2012">-rgrgrg</Event>
<Event Month="9" Day="11" Year="2012">-kollle holle</Event>
<Event Month="9" Day="25" Year="2012">-yapapajaadu</Event>
<Event Month="9" Day="26" Year="2012">-yeah geile mucke</Event>
<Event Month="9" Day="26" Year="2012">-Radiowecker</Event>
Then I get the values for Month, day and year from Rainmeter. And if a line match those values, that line should be deleted.
Example: Month=9, Day=11, Year=2012 ... The new notes.hol should look like this then
<Event Month="7" Day="10" Year="2012">-geilomatiko</Event>
<Event Month="7" Day="10" Year="2012">-come on</Event>
<Event Month="9" Day="25" Year="2012">-yapapajaadu</Event>
<Event Month="9" Day="26" Year="2012">-yeah geile mucke</Event>
<Event Month="9" Day="26" Year="2012">-Radiowecker</Event>
;-------------------------------------------------------------------------

That's my script at the moment

Code: Select all

function Initialize()
end

function Update()

   --Retrieve Measure Settings
	Month=SELF:GetOption('Month','')
	Year=SELF:GetOption('Year','')

	MDay=SELF:GetOption('Day','')
	DayMeter=SKIN:GetMeter(MDay)
	Day=DayMeter:GetOption("Text")

	file=io.input(SKIN:MakePathAbsolute('Holidays/Notes.hol'), 'wb')
	if file then
		
		for line in io.lines() do
					
		word=string.gsub(line,'<Event Month="'..Month..'" Day="'..Day..'" Year="'..Year..'">.+</Event>','')
		
		print(word)
		
		--file:write(word)
	
	end

 end
io.close(file)
SKIN:Bang('!Refresh')

end
I done the "delete-part" with string.gsub, but then I get empty lines where the old lines were (this problem is second-rate).
The main problem is that I can't write "word" to my file. "file:write(word)" does nothing and io.write(word) crashs my Rainmeter.

print(word) looks like that:
DBUG (00:26:56.170) <Event Month="7" Day="10" Year="2012">-geilomatiko</Event>
DBUG (00:26:56.170) <Event Month="7" Day="10" Year="2012">-come on</Event>
DBUG (00:26:56.170)
DBUG (00:26:56.170)
DBUG (00:26:56.170) <Event Month="9" Day="25" Year="2012">-yapapajaadu</Event>
DBUG (00:26:56.170) <Event Month="9" Day="26" Year="2012">-yeah geile mucke</Event>
DBUG (00:26:56.170) <Event Month="9" Day="26" Year="2012">-Radiowecker</Event>
And if someone don't understand something properly, please ask. I know my english isn't the best.
Hope someone can help me.

EDIT: With "file:write(word)" I get "Script: DeleteNotes.lua:17: Bad file descriptor" in About.
Rainmeter - You are only limited by your imagination and creativity.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Delete from a file ?

Post by smurfier »

Code: Select all

function Update()
	--Retrieve Measure Settings
	Month=SELF:GetOption('Month','')
	Year=SELF:GetOption('Year','')

	MDay=SELF:GetOption('Day','')
	DayMeter=SKIN:GetMeter(MDay)
	Day=DayMeter:GetOption("Text")

	file=io.open(SKIN:MakePathAbsolute('Holidays/Notes.hol'), 'r')
	if file then
		local lines = {}
		for line in file:lines() do   
			line=string.gsub(line,'<Event Month="'..Month..'" Day="'..Day..'" Year="'..Year..'">.+</Event>','')
			if not line == '' then table.insert(lines, line) end
		end
		file:close()
		file=io.open(SKIN:MakePathAbsolute('Holidays/Notes.hol'), 'w')
		file:write(table.concat(lines, '\n'))
		file:close()
	end
 end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: Delete from a file ?

Post by AlC »

Thank you very much again, smurfier!

There was a little problem, the if statement
"if not line == '' then ... " doesn't fired. I don't know why.
But

Code: Select all

 for line in file:lines() do   
         line=string.gsub(line,'<Event Month="'..Month..'" Day="'..Day..'" Year="'..Year..'">.+</Event>','')

         table.insert(lines, line) end
works perfect !

;-------------------------------------------------------------------------

2 things:
Is there a way to avoid the empty lines in string.gsub, or can I delete empty lines ?

Instead of fire this script on every day, I want something like
1: "delete everything older than this date" example: older than Month=8, Day=30, Year=2012
or if 1 isn't realizable then
2: "delete everything in this month" example: everything in Month=8, Year=2012
I think this could be maybe realizable with a "for-loop". Or can I use if-statements in string.gsub ?

any ideas are very welcome.
Rainmeter - You are only limited by your imagination and creativity.
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Delete from a file ?

Post by Kaelri »

AlC wrote:2 things:
Is there a way to avoid the empty lines in string.gsub, or can I delete empty lines ?
The if not line == '' part was supposed to get rid of the blank lines. It probably didn't work because there were still some extra characters left over (such as \r, a carriage return).

I would try something like this:

Code: Select all

if line:match('<event') then table.insert(lines, line) end
That should add the lines to the table only if the string.gsub did not match the date given.
AlC wrote:Instead of fire this script on every day, I want something like
1: "delete everything older than this date" example: older than Month=8, Day=30, Year=2012
Try this:

Code: Select all

local OptionTime  = os.time{ month = Month, day = Day, year = Year }

for line in file:lines() do   
	local EventMonth, EventDay, EventYear = line:match('<Event Month="(%d-)" Day="(%d-)" Year="(%d-)">.+</Event>')

	local EventTime   = os.time{ month = EventMonth, day = EventDay, year = EventYear }

	if EventTime >= OptionTime then table.insert(lines, line) end
end