Now came again my turn to ask something.
I have a lua script file, which returns the size of a file, through a function. The path of the file which size I'd like to get is given as a parameter. The function does work perfectly as long as the file given as parameter has only (let's say) "normal" characters in its name / path. But if the name or path of the file contains some characters (like brackets [ or ] - but there probably might be more such problematic characters), I get an error in the log and the function returns nothing.
I'm attaching a package which contains a sample skin with the appropriate lua script file and three extremely simple images (in the @Resources\Images folder). The skin should return the size of those image files. The name of the third file contains brackets and accordingly the skin return the size of the first two images, but doesn't return the size of the last image, whose name contains the brackets. Question is if there is any workaround to get the script file to return the size of this file as well.
Thanks in advance for any help.
It is currently October 4th, 2023, 6:16 am
File size problem in lua
-
- Rainmeter Sage
- Posts: 15689
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
File size problem in lua
You do not have the required permissions to view the files attached to this post.
-
- Rainmeter Sage
- Posts: 2468
- Joined: March 23rd, 2015, 5:26 pm
Re: File size problem in lua
The problem isn't your Lua script, it is actually the skin. Rainmeter appears to be treating your brackets as a bang or something (like "[3]") and failing, and as a result your Lua function is never even being called for the third filename. To see this in action, change your Lua to this:
Code: Select all
function FileSize(File)
local CurrentImage, FullImg, OFile, size = '', '', '', 0
FullImg = File
if (FullImg == nil) or (FullImg == '') then
return -1
else
OFile = io.open(FullImg, 'r')
if OFile ~= nil then
print('Successfully opened file ' .. File)
--local current = OFile:seek() -- get current position
size = OFile:seek("end") -- get file size
--OFile:seek("set", current) -- restore position
OFile:close()
else
print('Failed to open file ' .. File)
end
return size or -1
end
end
Note: You do not need to track the current file pointer position or reset it since all you are doing is opening the file, moving to the end, and closing it. If you were trying to move the file pointer around, you would need it, but for what you are doing it's not necessary. I commented out those lines.
-
- Rainmeter Sage
- Posts: 2468
- Joined: March 23rd, 2015, 5:26 pm
Re: File size problem in lua
So, in short, this isn't a file size problem in Lua, it is a parsing problem in Rainmeter.
-
- Rainmeter Sage
- Posts: 15689
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: File size problem in lua
You might have right, however something is bothering me. Besides getting the file size, as described above, I have a String measure, which returns the name of the file, without the path. This measure is looking this way:SilverAzide wrote: ↑January 31st, 2022, 5:42 pm The problem isn't your Lua script, it is actually the skin. Rainmeter appears to be treating your brackets as a bang or something (like "[3]") and failing, and as a result your Lua function is never even being called for the third filename.
Code: Select all
[MeasureFileName]
Measure=String
String=[MeasureImage0]
RegExpSubstitute=1
Substitute="^(.*)\\(.*)\.(.*)$":"\2"
DynamicVariables=1
However, is there any solution? Can you find a solution?
Thank you for the information.
-
- Rainmeter Sage
- Posts: 2468
- Joined: March 23rd, 2015, 5:26 pm
Re: File size problem in lua
This is the behavior I would expect. Rainmeter isn't trying to parse brackets in this case, so it should work fine. It's only when the variable is being resolved in the Lua call does Rainmeter attempt to resolve "[3]" and blows up.balala wrote: ↑January 31st, 2022, 7:08 pm [...snip...]
[MeasureImage0] is a Quote plugin measure, which returns the path and name of an image file. From this, the [MeasureFileName] measure is extracting the name of the file. Correctly, even if the file name contains brackets. That's I thought that there is a lua script file issue.
This is a nasty problem; I'm not sure how you can handle it or work around it. Have you tried using the FileView plugin to get the file size instead of using Lua? Lua should be much simpler, but obviously this is not working in this case. The only other thing I can think of (which is a horrible idea) is using a Substitute to translate "[" and "]" to "[\x005B]" and "[\x005D]". You'd probably need to escape this too; does "[*\x005B*]" even work?
-
- Rainmeter Sage
- Posts: 2468
- Joined: March 23rd, 2015, 5:26 pm
Re: File size problem in lua
How about this idea.... Use Lua, but instead of specifying the filename, specify the name of the variable or measure that is holding the filename.
Code: Select all
function FileSize(measureName)
...
local File = SKIN:GetMeasure(measureName):GetValue()
...
end
-
- Rainmeter Sage
- Posts: 15689
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: File size problem in lua
Ok, thanks for all of your suggestions. Will give them a try tomorrow, because this evening I have no mor time to work with them (the midnight is approaching here). Will come back with what I get.SilverAzide wrote: ↑January 31st, 2022, 8:46 pm How about this idea.... Use Lua, but instead of specifying the filename, specify the name of the variable or measure that is holding the filename.
Then, in your skin, you'd use FileSize('MeasureImage0'). It's a lame approach, but it should work.Code: Select all
function FileSize(measureName) ... local File = SKIN:GetMeasure(measureName):GetValue() ... end
Thanks again.
-
- Rainmeter Sage
- Posts: 15689
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: File size problem in lua
First sorry for my extremely late reply. Unfortunately yesterday had no time to work with this, finally today had, so gave a try.

I think this would be the best way to achieve what I want, but unfortunately have no idea on how to do this. Do you have any idea? An example would be nice. What I can't realize is how to get the size of a file with a defined name and path with the FileView plugin.SilverAzide wrote: ↑January 31st, 2022, 8:36 pm Have you tried using the FileView plugin to get the file size instead of using Lua?

-
- Rainmeter Sage
- Posts: 15689
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: File size problem in lua
I think I found a solution. No need for the lua script to get the file size. A RunCommand plugin measure, with proper Parameter option is enough and works. This one:
Works well, even if the file name or path contain brackets. No error messages, so problem solved, it seems.
Thank you SilverAzide for the assistance. Means a lot.
Code: Select all
[MeasureFileSize]
Measure=Plugin
Plugin=RunCommand
Parameter=for %I in ("#File#") do @echo %~zI
State=Hide
Thank you SilverAzide for the assistance. Means a lot.

-
- Rainmeter Sage
- Posts: 2468
- Joined: March 23rd, 2015, 5:26 pm
Re: File size problem in lua
Wow! I had no idea you could get file sizes that way!balala wrote: ↑February 2nd, 2022, 9:20 pm I think I found a solution. No need for the lua script to get the file size. A RunCommand plugin measure, with proper Parameter option is enough and works. This one:Works well, even if the file name or path contain brackets. No error messages, so problem solved, it seems.Code: Select all
[MeasureFileSize] Measure=Plugin Plugin=RunCommand Parameter=for %I in ("#File#") do @echo %~zI State=Hide
Thank you SilverAzide for the assistance. Means a lot.![]()