How do I read a file into a variable?

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

How do I read a file into a variable?

Post by Wolfie »

Very simple. How do I read a file into a variable but treat it like it's importing code.

Code: Select all

A:Apple,
B:Banana,
S:Steak,
To have this...

Groovy: Select all

map=(file contents)
return map['A']
(returns "Apple")
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: I got a new one for ya BlueNoah

Post 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

Format: Select all

{
	def table = csv('/path/to/names.tsv')
	table[n] ?: n
}

Console Output: Select all

$ cat ~/names.tsv
Deep Space 9	DS9
How I Met Your Mother	HIMYM

Screenshot
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: I got a new one for ya BlueNoah

Post 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

Format: Select all

{
	def table = csv('/path/to/names.tsv')
	table[n] ?: n
}

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.

Format: Select all

	def root="P:", config="P:/FileBot/"
	
	def maps = evaluate( new File( config+"config.txt" ) )[0]
	def map = maps['DEFAULTS'] + maps['MOVIES'], folders=map['folders'], dd=map['dd'], collectionsFolder=map['collectionsFolder']

Format: Select all

(config.txt)
[[
	"DEFAULTS":[
		"dd":"06",
		"folders":[
			"01":"_01",
			"02":"_02",
			"03":"_03",
			"04":"_04",
			"05":"_05",
			"06":"_06",
			"E1":"_E1",
			"E2":"_E2",
			"E3":"_E3",
		],
	],

	"TV":[
		"talkshows":true,
	],

	"MOVIES":[
		"collectionsFolder":"_",
	]
]]
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

Post by rednoah »

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


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


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


:idea: 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.
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post by Wolfie »

rednoah wrote: 23 Jul 2024, 04:21 :idea: 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.
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...

Format: Select all

{
	@P:/FileBot/config.groovy
}
How can I do that without throwing an error, and access the variables that were defined inside of it?
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

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

Format: Select all

{
@P:/FileBot/groovy-snippet.groovy
}
YES:

Format: Select all

@P:/FileBot/format-snippet.groovy
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post by Wolfie »

Format: Select all

def configs=include "P:/FileBot/config.groovy"
That seems to be working.

Also, did "stripReleaseInfo()" get removed?
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

Post by rednoah »

String.stripReleaseInfo(boolean strict) does exist. There is no parameterless method of that name though.
:idea: Please read the FAQ and How to Request Help.
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

Post by rednoah »

Wolfie wrote: 23 Jul 2024, 05:02

Format: Select all

def configs=include "P:/FileBot/config.groovy"
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.
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post by Wolfie »

rednoah wrote: 23 Jul 2024, 05:04 String.stripReleaseInfo(boolean strict) does exist. There is no parameterless method of that name though.
So value.stripReleaseInfo() won't work, and need to pass a paramater?

rednoah wrote: 23 Jul 2024, 05:08
Wolfie wrote: 23 Jul 2024, 05:02

Format: Select all

def configs=include "P:/FileBot/config.groovy"
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. 😁
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

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

Format: Select all

{
	'[ONe]_Ano_Hana_01_(1280x720)'.stripReleaseInfo(false)
}
That said, you're probably looking for the clean() method:

Format: Select all

{
	'[ONe]_Ano_Hana_01_(1280x720)'.clean()
}


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.
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post 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. 😊
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

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


:idea: 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.
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post 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. 😊
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

Post by rednoah »

Cheers to that! 🍻
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post by Wolfie »

rednoah wrote: 23 Jul 2024, 07:59 Cheers to that! 🍻
Found it...

viewtopic.php?p=22576#p22576
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post 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 "\."
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

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


:idea: 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.
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post by Wolfie »

Trying to do it for regex purposes, like "\d" to match a digit.
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

Post by rednoah »

If you use Groovy Syntax › 4.6. Slashy string then you don't need to escape \ characters:

Groovy: Select all

/\d/

If you use ' or " then Groovy Syntax › 4.3.1. Escaping special characters is relevant:

Groovy: Select all

"\\d"
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 128
Joined: 27 Oct 2015, 02:59

Re: How do I read a file into a variable?

Post by Wolfie »

Format: Select all

return /\d/
Returns /d

Same if I do '\\d' or "\\d"
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How do I read a file into a variable?

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

Format: Select all

{ "12345" ==~ /\d+/ }
:idea: Please read the FAQ and How to Request Help.
Post Reply