Page 1 of 1
How do I read a file into a variable?
Posted: 22 Jul 2024, 22:22
by Wolfie
Very simple. How do I read a file into a variable but treat it like it's importing code.
To have this...
Groovy: Select all
map=(file contents)
return map['A']
(returns "Apple")
Re: I got a new one for ya BlueNoah
Posted: 23 Jul 2024, 03:16
by rednoah
You can use the
csv() function to read a CSV / TSV file into a Map object.
e.g.
rednoah wrote: ↑15 Jul 2012, 09:03
Console Output: Select all
$ cat ~/names.tsv
Deep Space 9 DS9
How I Met Your Mother HIMYM
Re: I got a new one for ya BlueNoah
Posted: 23 Jul 2024, 04:08
by Wolfie
rednoah wrote: ↑23 Jul 2024, 03:16
You can use the
csv() function to read a CSV / TSV file into a Map object.
e.g.
rednoah wrote: ↑15 Jul 2012, 09:03
Console Output: Select all
$ cat ~/names.tsv
Deep Space 9 DS9
How I Met Your Mother HIMYM
Will need to take a look at that. So does a csv file have to have a "tsv" extension?
I came across the idea of evaluating a file to convert it to a string. This is more inline with what I'm looking for, though would be nice to not need to not have to do the extra assignments seen in the second half of the 3rd def line. Useful for adding additional values without having to def them, but if it can't be done, then oh well.
Would make it easier to alter GUI and automated options without having to edit both just to change a value.
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 04:21
by rednoah

The file path / file extension does not matter. But the file must be a tab-separated / comma-separated / pipe-separated / semicolon-separated / etc text file.

If you're reading in a complex configuration file
(i.e. nested structure, values, lists, maps, etc) then you could use the
json() function.

If you're dynamically executing external Groovy code at runtime
(not recommended) then you'll want to follow the
Split code into external *.groovy script files › External Groovy runtime includes examples.

If it's more about sharing a format between GUI and CLI, then
How can I use the same format in both GUI and CLI? is the way to go.
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 04:54
by Wolfie
Problems.
If I make the file and have the contents within {}'s, it's useless because I can't access the values.
If I don't put the contents within {}'s, it displays as part of the result (name).
If I include the @ line within {}'s, it gets mad at me saying that "@P...." was unexpected.
So...
How can I do that without throwing an error, and access the variables that were defined inside of it?
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 05:01
by rednoah
Split code into external *.groovy script files › External format @lines are FileBot-specific preprocessor instructions, i.e. you're effectively copy & pasting text files together, before passing the result as a whole to the Groovy compiler / interpreter that is completely unaware how the code came together. The
@line notably must
not start / end with spaces.
YES:
YES:
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 05:02
by Wolfie
That seems to be working.
Also, did "stripReleaseInfo()" get removed?
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 05:04
by rednoah
String.stripReleaseInfo(boolean strict) does exist. There is no parameterless method of that name though.
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 05:08
by rednoah
If you're using
include() just to create a structured data object for your configuration, then you definitely want to use
json() (and convert your configuration file to JSON syntax) instead for this particular use case.
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 05:24
by Wolfie
So value.stripReleaseInfo() won't work, and need to pass a paramater?
rednoah wrote: ↑23 Jul 2024, 05:08
If you're using
include() just to create a structured data object for your configuration, then you definitely want to use
json() (and convert your configuration file to JSON syntax) instead for this particular use case.
Doesn't that make it more complicated with having to use {}'s and []'s?
Also, letting the csv simmer in the back of my mind for making easy work of collections.

Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 05:41
by rednoah
Wolfie wrote: ↑23 Jul 2024, 05:24
So value.stripReleaseInfo() won't work, and need to pass a parameter?
Yep, must pass a parameter:
That said, you're probably looking for the
clean() method:
Wolfie wrote: ↑23 Jul 2024, 05:24
Doesn't that make it more complicated with having to use {}'s and []'s?
Groovy syntax is definitely more complicated than JSON syntax, because the former does a lot more, and the latter is just meant to be a simple structured data notation. That said, you can use whatever works best for you.
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 07:00
by Wolfie
rednoah wrote: ↑23 Jul 2024, 05:41
Wolfie wrote: ↑23 Jul 2024, 05:24
Doesn't that make it more complicated with having to use {}'s and []'s?
Groovy syntax is definitely more complicated than JSON syntax, because the former does a lot more, and the latter is just meant to be a simple structured data notation. That said, you can use whatever works best for you.
I'm tweaking and cleaning my expressions with the idea of sharing them. Want them to be simple include a few features that can benefit others. Such as being able to more or less "copy/paste" parts of it and only altering a few values to customize it for their use. So trying to keep it as simple as possible (the external file that will have the editable values). I figure it can also help others with making their own expressions by giving them ideas on how to do different things.
The other benefit is that the same file can be used across the GUI and command line, and between TV/movie expressions. Edit one file to update all four (both GUI, both command line). That map of folders that I showed earlier (config.txt) was something new I came up with to make my expressions more flexible. Idea is being able to put media files into a folder and that folder name will help determine which drive to copy/move the files to when renaming. Old method just keeps the names mostly the same, new one allows the folders to be "ID's" with better destination control.

Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 07:31
by rednoah
If it's
just about sharing data, then
csv() json() and friends will work best,
but if you also need to share code / logic
(in addition to the data that code is working with) then the
How can I use the same format in both GUI and CLI? is of course the way to go.

The
@lines (compile-time pre-processor instructions that generate code) approach is easier to understand
(once you understand that it's just plain/text copy & paste) and straight-forward, while the
include() (runtime includes that run code) just run code in the Groovy context, which may be more or less intuitive depending on your Groovy programming background. Notably, the latter is magnitudes slower and runs for each invocation but that probably doesn't matter.
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 07:52
by Wolfie
Using @lines already. But I might swap the order of how it's working. Idea is that sometimes, movies aren't considered part of a collection even though they should be. Not FileBot's fault, of course, as it's relying on the information it grabs. So idea is to allow for fixing that, or for overriding collections that may already exist. Same with TV shows, except it would be more about grouping. Like if someone wants to have certain shows (say "Arrowverse" shows) go into a special folder vs a default TV folder, then it could be done. I'm sorting it out with DC vs Marvel now, so that they are separated from regular shows, both into their own folder. All shows up in Plex the same, but at least I know where all the files should be at. Someone may want to do it with Star Wars, Star Trek, etc. Looking to provide an idea others can copy to make it work for them. The way I'm doing it now is how I learned from another post on here several years ago. You might have been the one to tell it to me, TBH. It works but I'm thinking of another way to do it.
Btw, thank you for all your help over the years. Great product and help, and I know I can count on you to teach how to do weird things.

Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 07:59
by rednoah
Cheers to that!

Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 08:00
by Wolfie
rednoah wrote: ↑23 Jul 2024, 07:59
Cheers to that!
Found it...
viewtopic.php?p=22576#p22576
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 18:18
by Wolfie
Okay... how do I get a backslash to stay a backslash when it's not being used in a pathname? No matter what I try, "\" is being changed to "/" when I need it to remain a "\."
Re: How do I read a file into a variable?
Posted: 23 Jul 2024, 19:08
by rednoah
Wolfie wrote: ↑23 Jul 2024, 18:18
I need it to remain a "\."

Why? What are you trying to achieve?
/ and
\ cannot not be used as path separator in a file path.
/ and
\ are notably interchangeable. You can do
/. or
\. and on Unix that would refer to the file system root, but on Windows it's just an invalid file path.

The FileBot Desktop application will notably use
/ when displaying file paths for consistency across platforms. Since
/ and
\ are interchangeable, that does not matter.
EDIT:

Are you perhaps talking about
Groovy Syntax › 4.3.1. Escaping special characters in String literals? Escape sequence
\\ will resolve to the backslash character.
Re: How do I read a file into a variable?
Posted: 24 Jul 2024, 01:14
by Wolfie
Trying to do it for regex purposes, like "\d" to match a digit.
Re: How do I read a file into a variable?
Posted: 24 Jul 2024, 03:51
by rednoah
If you use
Groovy Syntax › 4.6. Slashy string then you don't need to escape
\ characters:
If you use
' or
" then
Groovy Syntax › 4.3.1. Escaping special characters is relevant:
Re: How do I read a file into a variable?
Posted: 24 Jul 2024, 06:31
by Wolfie
Returns /d
Same if I do '\\d' or "\\d"
Re: How do I read a file into a variable?
Posted: 24 Jul 2024, 08:41
by rednoah
If you're testing in the format editor then you are confusing the actual value with the display file path.
You'll want to check if your regex pattern matches like so: