It is currently March 28th, 2024, 12:55 pm

File size problem in lua

Discuss the use of Lua in Script measures.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

File size problem in lua

Post by balala »

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.
FileSize.png
Thanks in advance for any help.
Attachments
FileSize_1.rmskin
(2.17 KiB) Downloaded 33 times
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: File size problem in lua

Post by SilverAzide »

balala wrote: January 31st, 2022, 5:05 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. 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
In the Rainmeter log window you will see the first and second files are opened, but the third is never even being called.

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.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: File size problem in lua

Post by SilverAzide »

So, in short, this isn't a file size problem in Lua, it is a parsing problem in Rainmeter.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: File size problem in lua

Post by balala »

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

Code: Select all

[MeasureFileName]
Measure=String
String=[MeasureImage0]
RegExpSubstitute=1
Substitute="^(.*)\\(.*)\.(.*)$":"\2"
DynamicVariables=1
[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.
However, is there any solution? Can you find a solution?
Thank you for the information.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: File size problem in lua

Post by SilverAzide »

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 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 However, is there any solution? Can you find a solution?
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?
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: File size problem in lua

Post by SilverAzide »

balala wrote: January 31st, 2022, 7:08 pm However, is there any solution? Can you find a solution?
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
Then, in your skin, you'd use FileSize('MeasureImage0'). It's a lame approach, but it should work.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: File size problem in lua

Post by balala »

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.

Code: Select all

function FileSize(measureName)
  ...
  local File = SKIN:GetMeasure(measureName):GetValue()
  ...
end
Then, in your skin, you'd use FileSize('MeasureImage0'). It's a lame approach, but it should work.
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.
Thanks again.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: File size problem in lua

Post by balala »

First sorry for my extremely late reply. Unfortunately yesterday had no time to work with this, finally today had, so gave a try.
SilverAzide wrote: January 31st, 2022, 8:36 pm Have you tried using the FileView plugin to get the file size instead of using Lua?
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. :confused:
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: File size problem in lua

Post by balala »

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:

Code: Select all

[MeasureFileSize]
Measure=Plugin
Plugin=RunCommand
Parameter=for %I in ("#File#") do @echo %~zI
State=Hide
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. :thumbup:
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: File size problem in lua

Post by SilverAzide »

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:

Code: Select all

[MeasureFileSize]
Measure=Plugin
Plugin=RunCommand
Parameter=for %I in ("#File#") do @echo %~zI
State=Hide
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. :thumbup:
Wow! I had no idea you could get file sizes that way!
Post Reply