It is currently July 23rd, 2024, 9:54 am

File names to string

Discuss the use of Lua in Script measures.
User avatar
pds
Posts: 79
Joined: April 12th, 2014, 12:52 pm
Location: Slovakia

File names to string

Post by pds »

Hi.
I would like to ask for help with a script that reads all filenames (without extensions) and puts them into a single string separated by "|"

For example :
directory Bands contains:
Linkin Park.png
Rammstein.png
Metallica.png
Guns N' Roses.png


and i need output like this : Linkin Park|Rammstein|....etc....
Last edited by pds on March 26th, 2023, 7:24 am, edited 1 time in total.
User avatar
Brian
Developer
Posts: 2713
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: File names to string

Post by Brian »

The trouble is, Lua in Rainmeter isn't going to be able to get a list of file names from a folder by itself. So you will have to have Rainmeter do that part for you.

One way would be to use the RunCommand plugin, running the command dir /B on the folder, then using a regular expression substitute, you could build your string as you want

Note: This assumes your folder only contains image files (like jpg or png, etc.)

Code: Select all

[Rainmeter]
DynamicWindowSize=1

[Variables]
Folder=#@#Images

[RunCommand]
Measure=Plugin
Plugin=RunCommand
Parameter="dir /B #Folder#"
FinishAction=[!SetOption Test Text "[RunCommand]][!UpdateMeter Test][!Redraw]
RegExpSubstitute=1
Substitute="#CRLF#":"|","\.(jpg|jpeg|gif|png)":"","\|$":""

[Test]
Meter=String
SolidColor=0,0,0,255
FontColor=255,255,255,255
Padding=5,2,5,2
Text=Click Me...
LeftMouseUpAction=[!CommandMeasure RunCommand "Run"]
DynamicVariables=1
All that does is when the Test meter is clicked, it runs the dir /B command on the folder (you will have to edit the Folder variable). Then the substitute changes the newlines to |, removes the image extension, and removes the final | (since the dir command adds an extra newline at the end).

This solution won't work if there are other file types in the folder. In that case, you may have use a more clever regular expression to get rid of it, or jump out to LUA and remove those from the string.

-Brian
User avatar
nek
Posts: 106
Joined: November 3rd, 2019, 12:00 am

Re: File names to string

Post by nek »

I was just writing the similar code with Brian.
something.png
Skin.ini

Code: Select all

[Rainmeter]
Update=-1
;DefaultUpdateDivider=-1
AccurateText=1
MouseActionCursor=0
OnRefreshAction=[!Delay 400][!CommandMeasure pCommandDir "Run"]

[Variables]
;; Skin scaling
$=1.00
FONT_SIZE=15
PATH_FOLDER=%USERPROFILE%\Pictures\Bands

[sScript]
Measure=Script
ScriptFile=#@#Scripts\misc.lua
Disabled=1

;; RunCommand plugin | https://docs.rainmeter.net/manual/plugins/runcommand/
[pCommandDir]
Measure=Plugin
Plugin=RunCommand
Parameter=dir "#PATH_FOLDER#" /A-D /B
RegExpSubstitute=1
;
; Substitute="(?:^\s+|\s+$)":"", "(?m)\.\w+$":"", "#CRLF#":"|"
; > `\s+$` is not good for performance.
; Substitute="^\s+":"", "(?s)^(.*\S)\s+$":"\1", "(?m)\.\w+$":"", "\n":"|"
; > `(.*\S)` capturing is not good for performance.
; Substitute="^\s+":"", "^[\s\S]*\S\K\s+$":"", "(?m)\.\w+$":"", "\n":"|"
; > If there are not any whitespace characters at the end of string, `^[\s\S]*\S\K\s+$` will take more steps to parse.
; Substitute="\A\s+":"", "\A[\s\S]*\S\K(?(?=\s)\s+)\z":"", "(?m)\.\w+$":"", "\n":"|"
; > tweaked
; Substitute="\A\s+":"", "\A[\s\S]*\S\K(*SKIP)\s+\z":"", "(?m)\.\w+$":"", "\n":"|"
; > tweaked
;
Substitute="\A\s+":"", "\A[\s\S]*\S\K\s*\z":"", "(?m)\.\w+$":"", "\n":"|"
FinishAction=[!SetOption FileNames Text "[pCommandDir]"][!UpdateMeter FileNames][!Redraw][!CommandMeasure sScript "output('#CURRENTSECTION#')"]

[FileNames]
Meter=String
AntiAlias=1
FontFace=Segoe UI
FontSize=(#FONT_SIZE#*#$#)
FontColor=171717
FontWeight=600
Padding=(8*#FONT_SIZE#/15*#$#),0,(8*#FONT_SIZE#/15*#$#),0
Prefix=File names:[\x0020]
SolidColor=F5F5F5
Text=
RegExpSubstitute=1
Substitute=
"\A\s+":"" Trim whitespace characters at the very beginning of the string.
\A Very beggining of the string
\s Any whitespace character. [\r\n\t\f\v ]
https://regex101.com/r/f7SNq9/1

"\A[\s\S]*\S\K\s*\z":"" Trim whitespace characters at the very end of the string. Parsing from the end of string.
\S Non-whitespace character
\K Reset current match
\z Very end of the string
https://regex101.com/r/f7SNq9/2

"(?m)\.\w+$":"" Remove file extensions.
(?m) ^ and $ match start and end of the line
\w Any word character [0-9A-Za-z_]
$ End of the line (string, depend on the mode)
https://regex101.com/r/f7SNq9/3

"\n":"|" Replace new line character with |
https://regex101.com/r/f7SNq9/4



@Resources\Scripts\misc.lua

Code: Select all

--
-- http://lua-users.org/wiki/SplitJoin
-- @param sep {string}  -  separator
-- @return    {table}   -  Lua table index start at `1` not `0`
--
function string:split(sep)
  local sep, fields = sep or ":", {}
  local pattern = string.format("([^%s]+)", sep)
  self:gsub(pattern, function(c) fields[#fields+1] = c end)
  return fields
end

function output(section_name)
  local file_names = SKIN:GetMeasure(section_name):GetStringValue()
  local files = file_names:split('|')
  for i=1, #files do
    print(files[i])
  end
end


📗 Measure Substitute, RunCommand plugin, Lua Scripting > Measure object
PCRE Regex Cheatsheet | https://www.debuggex.com/cheatsheet/regex/pcre
PCRE syntax | https://www.rexegg.com/pcre-doc/08.37/pcresyntax.html
Online Regex Testing | https://regex101.com | FLAVOR PCRE (PHP <7.3) FUNCTION Substitusion REGEX FLAGS global
You do not have the required permissions to view the files attached to this post.
Last edited by nek on June 5th, 2023, 6:39 pm, edited 10 times in total.
User avatar
pds
Posts: 79
Joined: April 12th, 2014, 12:52 pm
Location: Slovakia

Re: File names to string

Post by pds »

Thank you both for your help. It works.
User avatar
Yincognito
Rainmeter Sage
Posts: 7769
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: File names to string

Post by Yincognito »

A slower alternative could be using powershell as a command and either (dir | % {$_.basename}) -join '|' or gci -r | % {$_.Name -join '|'} as a parameter, without any substitutes or Lua. The cmd.exe variant is preferable though, since it's much faster, at least on first run.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth