It is currently April 16th, 2024, 9:48 pm

Is there a way to overwrite a text file?

Discuss the use of Lua in Script measures.
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Is there a way to overwrite a text file?

Post by jav26122 »

I made a skin that reads certain things that get put in a text file and then displays them in a more friendly manner. The problem is, the text file eventually gets very big because it's a log file and adds a new line of logs every second. It gets to a point where rainmeter is maxing out cpu usage in order to go through the file because it gets so big.
So my question is, is there a way to delete the context of the text file in the lua script? I've messed around with file.open(w) and file.write() but nothing seems to work for some reason.
User avatar
balala
Rainmeter Sage
Posts: 16142
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Is there a way to overwrite a text file?

Post by balala »

Something like this could help:

Code: Select all

FilePath = SKIN:MakePathAbsolute('YourFile.txt')
local File = io.open(FilePath, 'w')
File:write('')
File:close()
This will leave your file empty.
The letter on the io.open command is what you need to change depending on what you'd like to do:
- w will overwrite the file
- r will read the file
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »

Wow I'm pretty sure I've tried that before but it works now... Thanks
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

'r': read mode (the default);
'w': write mode;
'a': append mode;
'r+': update mode, all previous data is preserved;
'w+': update mode, all previous data is erased;
'a+': append update mode, previous data is preserved, writing will be at the end of file.

The difference as I understand it is that the "w+" mode keeps the existing file, which whatever permissions and other attributes it might have, while the "w" in effect just deletes the old file and creates a new one. Not sure there is really a difference in a Windows environment.
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »

jav26122 wrote:Wow I'm pretty sure I've tried that before but it works now... Thanks
I spoke too soon... Now I remember the problem I was having before.
The file gets overwritten but it doesn't really... save I guess?

It's really hard to explain so I just recorded what happens. Basically the file is clearly getting overwritten with nothing every time the script gets refreshed by rainmeter, but it just keeps coming back and continues to grow bigger and bigger. I've tried w+ but the same thing happends.

It's kind of hard to see but you should get the point
https://www.youtube.com/watch?v=yKGKV_uoYt0
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

I'd have to see the code.
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »

If you havn't already figured it out, it's to display the information in the gpu-z logfile.

Code: Select all

function Initialize()
	Filename = SELF:GetOption('LogFile')
	Filename = SKIN:MakePathAbsolute(Filename)
end

function Update()
	local File = io.open(Filename)
	if not File then return end

	local Contents = File:read("*all")
	File:close()
	
	local Main = {}
	for line in Contents:gmatch('[^\n]+') do
		table.insert(Main, line)
	end
	
	local File = io.open(Filename, 'w+')
	File:write('')
	File:close()

	local Latest = Main[#Main]
	
	local Datestart = string.find(Latest, ',')
	local CCstart = string.find(Latest, ',', Datestart+1)
	local MCstart = string.find(Latest, ',', CCstart+1)
	local Tempstart = string.find(Latest, ',', MCstart+1)
	local MemUstart = string.find(Latest, ',', Tempstart+1)
	local Loadstart = string.find(Latest, ',', MemUstart+1)
	local MemLoadstart = string.find(Latest, ',', Loadstart+1)
	local VEloadstart = string.find(Latest, ',', MemLoadstart+1)
	local PCstart = string.find(Latest, ',', VEloadstart+1)
	local Voltagestart = string.find(Latest, ',', PCstart+1)

	local Date = string.sub(Latest, 1, Datestart-1)
	local CoreClock = string.sub(Latest, Datestart+1, CCstart-1)
	local MemClock = string.sub(Latest, CCstart+1, MCstart-1)
	local Temp = string.sub(Latest, MCstart+1, Tempstart-1)
	local MemUsed = string.sub(Latest, Tempstart+1, MemUstart-1)
	local Load = string.sub(Latest, MemUstart+1, Loadstart-1)
	local MemLoad = string.sub(Latest, Loadstart+1, MemLoadstart-1)
	local VELoad = string.sub(Latest, MemLoadstart+1, VEloadstart-1)
	local PerfCap = string.sub(Latest, VEloadstart+1, PCstart-1)
	local Voltage = string.sub(Latest, PCstart+1, Voltagestart-1)
	
	local CoreClockg = CoreClock:gsub(" ", "")
	local Loadg = Load:gsub(" ", "")
	local Loadgn = tonumber(Loadg)
	local Voltageg = Voltage:gsub(" ", "")
	local Tempg = Temp:gsub(" ","")
	local Tempgn = tonumber(Tempg)
	local MemUsedg = MemUsed:gsub(" ", "")
	local MemUsedgn = tonumber(MemUsedg)
	MemUsedg2 = tostring(math.floor((MemUsedgn / 2048) * 100))

	SKIN:Bang('!SetOption', 'GPUMHz', 'Text', CoreClockg)
	SKIN:Bang('!SetOption', 'MeterGPU', 'Text', Loadg)
	SKIN:Bang('!SetOption', 'MeasureGPU', 'Formula', Loadgn)
	SKIN:Bang('!SetOption', 'MeterVoltageText', 'Text', Voltageg.."V  "..Tempg.."C")
	SKIN:Bang('!SetOption', 'MeasureGPUTemp', 'Formula', Tempgn)
	SKIN:Bang('!SetOption', 'MemText%', 'Text', (MemUsedg2).."%")
	SKIN:Bang('!SetOption', 'MeterFreeMemText', 'Text', MemUsedg.."MB")
	SKIN:Bang('!SetOption', 'MeasureVRAM', 'Formula', MemUsedgn)
	SKIN:Bang('!SetOption', 'MeasureGPULoad', 'Formula', Loadgn)
end
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

Looks fine to me. I think that file must end up as a zero byte file at the end of that Update().

It may be possible that whatever process is creating and adding to the log file opens it in exclusive write/append mode, which means a different application won't be able to modify it while it is open.
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »

It doesn't though. The file will continue to get bigger and take more and more cpu to go through it every second. As soon as I unload the rainmeter skin the file size is bigger.

So I started the rainmeter skin about a half hour ago and the file was about 400kb. While the skin is running, the file appears to be 0 but sometimes for a split second you can see it display the actual size. I just unloaded the skin and now it's 811kb. Once it gets around 50+mb rainmeter will be constantly using 25% of my cpu.

It appears like it's getting overwritten when the skin is running, but it's actually not. That's the problem I have.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

It may be possible that whatever process is creating and adding to the log file opens it in exclusive write/append mode, which means a different application won't be able to modify it while it is open.