Check if the folder exists

All about user-defined episode / movie / file name format expressions
Post Reply
DIK
Posts: 8
Joined: 26 Jan 2019, 20:43

Check if the folder exists

Post by DIK »

I have some quantity of folders for TV serials (ex. /path/first/, /path/second/, /path/third/).
And now I use:

Code: Select all

seriesFormat={path/s3/{n.replaceTrailingBrackets().colon(" -").slash(" - ").upperInitial().replaceAll(/^The (.*)$/, '$1, The')}/{s}{e.pad(2)}. {t} ({airdate})
let N will be the name of subfolder, that is

Code: Select all

{n.replaceTrailingBrackets().colon(" -").slash(" - ").upperInitial().replaceAll(/^The (.*)$/, '$1, The')} 
let F will be the new file name, that is

Code: Select all

{s}{e.pad(2)}. {t} ({airdate})
in these terms I have

Code: Select all

seriesFormat=path/s3/N/F
How to get this in one expression:

Code: Select all

if in /path/first/ exists folder N
  then seriesFormat should be "path/first/N/F"
else if in /path/second/ exists folder N
  then seriesFormat should be "path/second/N/F"
else
  seriesFormat will be as usual "path/third/N"
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Check if the folder exists

Post by rednoah »

Please check out the Sort into drive with the most free space example here:
viewtopic.php?f=5&t=2

e.g.

Code: Select all

{
	def N = plex[1].upperInitial().sortName()
	def F = plex.derive{ " ($airdate)"}.name

	['/path/1st' / N, '/path/2nd' / N, '/path/3rd' / N].max{ a, b -> a.exists() <=> b.exists() ?: a.diskSpace <=> b.diskSpace } / F
}
:idea: Please read the FAQ and How to Request Help.
DIK
Posts: 8
Joined: 26 Jan 2019, 20:43

Re: Check if the folder exists

Post by DIK »

Sorry, I need only check existence of folders "path/first/N" and "path/second/N".
How to read this: a.diskSpace <=> b.diskSpace?
There is no need in checking disk space. We should select path ONLY on the fact of existence of the folders (in order):
if (N exists in first)
we select path/first/N
else if (N exists in second)
we select path/second/N
else
we select path/third/N
I need a string for filebot-node seriesFormat in Synology DSM WITHOUT plex server. Can I use plex in expressions? Are the def's you specified do the same as my long expressions?
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Check if the folder exists

Post by rednoah »

Just copy this format expression into the FileBot Node WebUI and see if it works:

Code: Select all

{def N = plex[1].upperInitial().sortName(); def F = plex.derive{ " ($airdate)"}.name; ['/path/1st' / N, '/path/2nd' / N, '/path/3rd' / N].max{ a, b -> a.exists() <=> b.exists() } / F}
:idea: Please read the FAQ and How to Request Help.
DIK
Posts: 8
Joined: 26 Jan 2019, 20:43

Re: Check if the folder exists

Post by DIK »

Is it possible to refer to "Output Folder" specified in FileBot-Node? Or I must use absolute path inside [.., .., ..].?
def N gives not the same I wrote initially. But this not a problem.
The expression don't works. It always give me first part (/path/1st/N above) but this folder not exists. In this case it should check second part (existence /path/2nd/N) and return it (if exists /path/2nd/N) or /path/3rd/N (if /path/2nd/N not exists).
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Check if the folder exists

Post by rednoah »

1.
Please copy & paste the FileBot Node WebUI console output, so I can see exactly what command is executed, and what the output says. Please read How to Request Help.


2.
Yes, the --output option value is available via the {output} binding, which is always an absolute path.

e.g.

Code: Select all

output / "1st" / N

3.
The code works as far as I can test.

Test 1: no folder exists:

Code: Select all

$ filebot -script "g:println(['1st', '2nd', '3rd']*.toFile().max{ a, b -> a.exists() <=> b.exists() })"
1st
Test 2: 3rd folder exists:

Code: Select all

$ mkdir 3rd
$ filebot -script "g:println(['1st', '2nd', '3rd']*.toFile().max{ a, b -> a.exists() <=> b.exists() })"
3rd
Test 3: 2nd and 3rd folder exist:

Code: Select all

$ mkdir 2nd 3rd
$ filebot -script "g:println(['1st', '2nd', '3rd']*.toFile().max{ a, b -> a.exists() <=> b.exists() })"
2nd
:idea: I'm testing the Groovy code directly, and not as part of a format, but as you can see, it works.
:idea: Please read the FAQ and How to Request Help.
DIK
Posts: 8
Joined: 26 Jan 2019, 20:43

Re: Check if the folder exists

Post by DIK »

Thanks. Yes, it works but in wrong order. I should reorder folders in [] (in not obvious order).
I found that this construction is better:

Code: Select all

{def N = plex[1].upperInitial().sortName(); ((output / 'first' / N).exists() ? 'first'  : …) / N}
And during the tests I found that even if in filebot-node Conflict is set to "Override existing files" filebot skips existing files.
How to force it override existing files?
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Check if the folder exists

Post by rednoah »

1.
The code above will pick the first existing folder from the list. That's what I had assumed you were looking for. If something else works better for your particular use case, then go with that.


2.
--conflict override will override existing files. If you're having trouble with that, please post console output that shows filebot not working as expected.
:idea: Please read the FAQ and How to Request Help.
DIK
Posts: 8
Joined: 26 Jan 2019, 20:43

Re: Check if the folder exists

Post by DIK »

In test mode on two files one of them already exists:

Code: Select all

filebot -script 'fn:amc' /volume2/Download --output /volume2 --action TEST --order Airdate --conflict override --lang en --def 'skipExtract=y' 'seriesFormat={def N = n.replaceTrailingBrackets().colon('\'' -'\'').slash('\'' - '\'').upperInitial().replaceAll(/^The (.*)$/, '\''$1, The'\''); ((output / '\''SciFi'\'' / N).exists() ? '\''SciFi'\'' : '\''Сериалы'\'') / N}/{s}{e.pad(2)}. {t} ({airdate})' --log all --log-file '/volume1/@appstore/filebot-node/data/filebot.log'

------------------------------------------

Locking /volume1/@appstore/filebot-node/data/filebot.log
Run script [fn:amc] at [Mon Jan 28 14:14:54 AST 2019]
Parameter: skipExtract = y
Parameter: seriesFormat = {def N = n.replaceTrailingBrackets().colon(' -').slash(' - ').upperInitial().replaceAll(/^The (.*)$/, '$1, The'); ((output / 'SciFi' / N).exists() ? 'SciFi' : 'Сериалы') / N}/{s}{e.pad(2)}. {t} ({airdate})
Argument[0]: /volume2/Download
Ignore system path: /volume2/Download/@eaDir
Ignore system path: /volume2/Download/#Torrents/@eaDir
Input: /volume2/Download/Future.Man.S02E01.rus.LostFilm.TV.avi
Input: /volume2/Download/The.Orville.S02E05.rus.LostFilm.TV.avi
Group: [tvs:future man] => [Future.Man.S02E01.rus.LostFilm.TV.avi]
Group: [tvs:the orville] => [The.Orville.S02E05.rus.LostFilm.TV.avi]
Rename episodes using [TheTVDB]
Auto-detected query: [Future Man]
Fetching episode data for [Future Man]
[TEST] from [/volume2/Download/Future.Man.S02E01.rus.LostFilm.TV.avi] to [/volume2/Сериалы/Future Man/201. Countdown to a Prologue (2019-01-11).avi]
Processed 1 files
Rename episodes using [TheTVDB]
Auto-detected query: [The Orville]
Fetching episode data for [The Orville]
Skipped [/volume2/Download/The.Orville.S02E05.rus.LostFilm.TV.avi] because [/volume2/SciFi/Orville, The/205. All the World is Birthday Cake (2019-01-24).avi] already exists
Processed 0 files
Done ヾ(@⌒ー⌒@)ノ
------------------------------------------
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Check if the folder exists

Post by rednoah »

Strange. What does sysinfo say?

:arrow: Please click the System Information button in the Tools menu and post the output.


EDIT:

Ah! The --conflict override option doesn't work in combination with --action test because override behaviour deletes files, which test mode prohibits.
:idea: Please read the FAQ and How to Request Help.
DIK
Posts: 8
Joined: 26 Jan 2019, 20:43

Re: Check if the folder exists

Post by DIK »

Ok, thanks.
Post Reply