INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Rename, Move & Copy Files uses "unknown" for blank fields  (Read 6915 times)

dreandre

  • Junior Woodchuck
  • **
  • Posts: 66
Rename, Move & Copy Files uses "unknown" for blank fields
« on: October 07, 2014, 06:40:05 pm »

Hello all,

I am creating a filename rule that includes a field that is sometimes left blank.  When I go to rename, the empty field is replaced in the name as "Unknown Field" (i.e. empty artist field is "Unknown Artist" in the file name").  I am trying to have the empty field be empty also in the file name as well.

Can anyone help with this? Thanks!
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42373
  • Shoes gone again!
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #1 on: October 07, 2014, 06:49:48 pm »

You can do this:
If(IsEmpty([Bios,0]),,[Bios])

Insert any field in place of Bios.
Logged
Matt Ashland, JRiver Media Center

dreandre

  • Junior Woodchuck
  • **
  • Posts: 66
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #2 on: October 07, 2014, 09:24:36 pm »

I apologize, Im not familiar with how to use that code.  Can you please walk me through it?  Thanks!
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #3 on: October 07, 2014, 10:56:54 pm »

MC has a built-in expression language, which allows you to transform the output from fields in a wide variety of ways.  It is quite powerful.  The file renaming rules can include expressions.

The default behavior is to fill "Unknown <Field Name>" into fields that turn out blank, when you include a Field directly in the Rename, Move, and Copy Files tool.  This is because (among other reasons) a pathname component cannot be completely blank. So if you did something like this in a directory rule:

\[Artist]\[Album]\[Disc #]

But you renamed a file that didn't contain a value in [Disc #] it would output:

\Pink Floyd\The Dark Side of the Moon\\03 - On The Run.flac

Or say you have one that doesn't contain either an Album title or a Disc #, then it would:

\ABBA\\\00 - Who Cares What Album This Track is From.flac

Same goes for the filename rule.  What if you put the filename rule as: [Track #] [Name]

And you rename a file that is missing both?  Do you get ".flac"?  That's no good.

In other places in MC, like in the Columns or in Theater View's details screen (and when evaluating all expressions) if you include a field that is empty, it returns a null string (no text at all).  But in places where MC will be writing filenames to disk, to prevent blank pieces of filenames, it has a special rule that replaces empty Fields with "Unknown".  More than anything else, it shows you that you made an error in the preview in the Rename, Move, and Copy Files dialog.  But note, it replaces empty Fields with this "Unknown" text.  It does not replace empty Expressions with anything (and actually outputs null text for those).  The special "you're renaming files exception" only applies to the raw value of Fields.

But that's all an explanation of why it is that way, and not what you can do about it.  You can do something about it, and that something is to write your own expression, which I'll explain in my next post, in concepts.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #4 on: October 07, 2014, 11:52:04 pm »

As I said above, MC includes an expression language that can be used to manipulate text, fields, the Auto-Import rules, and to create custom calculated fields.  It can do math, and has a wide variety of text manipulation functions, evaluate Regular Expressions, and all sorts of other crazy things.  Expressions can be used within the Search Language system in MC as well.

The Expression Language Wiki article I linked to is quite thorough and detailed (thanks MrC wherever you are), and can teach you all about the Expression Language.

But, for now... All that brings us to is what the heck Matt was telling you to do.

Well, that's actually pretty simple.  He's telling you to use a little expression instead of the default output for your field that sometimes ends up blank.  That will let you override the default behavior of the Rename, Move, and Copy Fields tool, because your expression is not a Field, so it is "allowed" to output blank.  It will output exactly what you tell it to output.  His expression is a simple one, and good for "starting off" which is why I took this opportunity to explain.  Just diving right into that Expression Language tome on the Wiki can be overwhelming.

First, the most important function in the Expression Language is probably the If() function.  It is conceptually simple.  It lets you test something, and output one thing if the result is True, and something else if the result is false.  It looks like:

If(<TEST>,<OUTPUT IF TRUE>,<OUTPUT IF FALSE>)

So, say you have a field called [Color] that can contain any color you want.  And then you make another calculated field (which is a special kind of custom field that is calculated for each file using an Expression) that you want to output the words "My Favorite Color" for any file where the [Color] field contains "blue"*.  And otherwise output "meh".  Your expression would be constructed like this using the IsEqual() function to test the [Color] field for equality with "blue":

Code: [Select]
If(IsEqual([Color],blue,1),My Favorite Color,meh)
The IsEqual() part of the expression is the <TEST> of the If() statement.  In this example, we test to see if [Color] == "blue" and if so, we output some text.  Otherwise, we output some other text.  The 1 that is part of the IsEqual() function tells IsEqual to run in case insensitive mode.  There are other modes.  Then, the rest just outputs the text we wanted.

Matt's expression doesn't use IsEqual() though.  Instead, it uses the IsEmpty() function as the <TEST> in the If().  This tests to see if the provided value is empty or not.  It returns 1 (True) if the provided value evaluates to empty, and 0 (False) if it does not.

So...

Code: [Select]
If(IsEmpty([Bios,0]),,[Bios])
This tests to see if [Bios] is empty.  If so, it actually outputs nothing at all.  See how there are double commas?  The <OUTPUT IF TRUE> part of the If() expression is completely blank.  So, if the result of the <TEST> is true, it outputs blank.  If the IsEmpty() test is False (and so then [Bios] does contain something useful), then it outputs the contents of [Bios].

So, this is a stand-in replacement for the way Rename, Move, and Copy Files normally treats [Bios] that does what you want it to do, and really outputs blank.  The last part that I should mention is the fact that the IsEmpty() test includes an odd zero after the name of the Bios field:

[Bios,0]

That's a special notation that tells MC to turn off the normal field processing that occurs for display and show the raw value.  For example, you can use it with [Date] to show the "real" internal date value (which work like dates in Excel).  Or, you know how if you set [Disc #] to zero, then it actually "vanishes"?  That's because for display (in normal "friendly" field-display mode) MC hides values that equal Zero.  If you want to see the Zero when it exists, then you can make MC output the "raw" value by doing [Disc #,0].

Now, it is unlikely that you'll use [Bios] in your Rename, Move, and Copy Files rule, so perhaps a more useful example is [Disc #].

Here's my rule for renaming Music Files:

Code: [Select]
Audio\Music\[Artist Letters]\[Artist]\[Album]\If(IsEmpty([Disc #],1),,[Disc #])
So, in that case, I only want it to make a Disc # "tier" of folders if the particular file being renamed contains a non-zero [Disc #] (note, I didn't use the [Disc #,0] notation there, because I'm testing to see if it is empty.  So, if I rename a file that does not have a disc number at all, then it doesn't get any folder at that level and the files are there.  If the album does contain disc numbers, then there is another "tier" of folders labeled 1, 2, 3, etc.


Click to embiggen.

See how Dark Side gets no Disc Number part of the path at all, but Ummagumma (which has multiple discs) does?

Your expressions in there can be as complex as you want.  If you have a particularly complex one, you can make the expression itself into a custom calculated Field, which basically works as a "shortcut" for the expression.  That's what I did above with the [Artist Letters] field.  That's a custom calculated field I made that uses the expression:

Code: [Select]
Regex([Artist], /#^(?:(?:the|an|a) +)?(.)#/,-1)if(Compare(1[R1], >=, 10), 0 - 9, [R1])
That ugly string of nonsense does this:



I hope all this makes sense, but read up on the wiki article a bit, play with it, and ask if you need to know more.

* No yelloooooooow!  ;)
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9139
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #5 on: October 08, 2014, 12:14:48 am »

Here's a similar post I made recently on this subject....

Good stuff, glad that worked for you.

Re: isempty()...

If you imagine asking MC to create a directory structure using [series] without first testing its 'emptiness', and it was empty, MC would be attempting to create a directory with no name, which is not good. For this reason, if the field is actually empty, the rename tool replaces the emptiness with "Unknown [field]", and it does this before handing things over to the expression evaluator, meaning that as far as its concerned, the field is not empty.

You can use the same method to turn this off, ie...
Quote
[Media Type]\[Media Sub Type]if(isempty([series,0]),,[series])\if(isempty([album,0]),[name],[album])
You just need to be careful you're not asking MC to create illegal file paths or names, although in my testing, MC appears to gracefully and quietly correct any illegal double backslashes that appear in the new path, so you just lose a level in the path, which might not be what you want, but would be easy to correct.

-marko

The full thread if you want the pre cursor: http://yabb.jriver.com/interact/index.php?topic=91141.msg627443#msg627443

fitbrit

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 4887
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #6 on: October 08, 2014, 01:17:38 am »

*applause*
Thanks to you both, glynor and Marko.
I will bookmark this thread.
Logged

Arindelle

  • Citizen of the Universe
  • *****
  • Posts: 2772
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #7 on: October 08, 2014, 07:01:24 am »

BRAVO guys to evernote it goes :)
Logged

dreandre

  • Junior Woodchuck
  • **
  • Posts: 66
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #8 on: October 09, 2014, 01:52:34 am »

OMG what a FANTASTIC ANSWER!!!!!!   :o

This speaks to what I want to happen, which is exactly the example that you gave, and it does it in a much better way that what I was attempting....

Thank you guys!!!
Logged

dreandre

  • Junior Woodchuck
  • **
  • Posts: 66
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #9 on: October 09, 2014, 02:28:14 am »

Just to finish out the post, here is what I have accomplished from the information!! 

The filename rule I was able to create looks like this:

[Artist]If(IsEmpty([Events,0]),,;  [Events]);  [Album]If(IsEmpty([Title,0]),,;  [Title]);  [Track #]If(IsEmpty([Name,0]),,;  [Name])

The attachment now shows the proper naming that I was shooting for!!!  SO excellent!!!  And you have set the example for me to learn how to create directory rules also....

Thanks!!!
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #10 on: October 09, 2014, 06:56:28 am »

Better to teach a man to fish...  ;D

No problem!
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

Vocalpoint

  • Citizen of the Universe
  • *****
  • Posts: 2007
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #11 on: October 09, 2014, 07:42:43 am »

Here's my rule for renaming Music Files:

Code: [Select]
Audio\Music\[Artist Letters]\[Artist]\[Album]\If(IsEmpty([Disc #],1),,[Disc #])
So, in that case, I only want it to make a Disc # "tier" of folders if the particular file being renamed contains a non-zero [Disc #] (note, I didn't use the [Disc #,0] notation there, because I'm testing to see if it is empty.  So, if I rename a file that does not have a disc number at all, then it doesn't get any folder at that level and the files are there.  If the album does contain disc numbers, then there is another "tier" of folders labeled 1, 2, 3, etc.

Your expressions in there can be as complex as you want.  If you have a particularly complex one, you can make the expression itself into a custom calculated Field, which basically works as a "shortcut" for the expression.  That's what I did above with the [Artist Letters] field.  That's a custom calculated field I made that uses the expression:

Code: [Select]
Regex([Artist], /#^(?:(?:the|an|a) +)?(.)#/,-1)if(Compare(1[R1], >=, 10), 0 - 9, [R1])
That ugly string of nonsense does this:




Wow. Now this is cool. This is exactly how I store my stuff - with a 26 folder/letter structure.

So this:

Regex([Artist], /#^(?:(?:the|an|a) +)?(.)#/,-1)if(Compare(1[R1], >=, 10), 0 - 9, [R1])

Is a custom field within the main library?

And - how to modify it to look at the first letter of an artists last name? This works awesome for Pink Floyd as it will put the files right into the P Folder - but if I use it on George Harrison - it wants to stick the files in the G folder - anyway to get it to go to H if possible?

VP
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Rename, Move & Copy Files uses "unknown" for blank fields
« Reply #12 on: October 09, 2014, 05:50:57 pm »

So this:

Regex([Artist], /#^(?:(?:the|an|a) +)?(.)#/,-1)if(Compare(1[R1], >=, 10), 0 - 9, [R1])

Is a custom field within the main library?

Yes.

It is a custom calculated field called [Artist Letters].  See:
http://yabb.jriver.com/interact/index.php?topic=72309.0

And - how to modify it to look at the first letter of an artists last name? This works awesome for Pink Floyd as it will put the files right into the P Folder - but if I use it on George Harrison - it wants to stick the files in the G folder - anyway to get it to go to H if possible?

There's not a 100% clean solution to automate this because no script or automation tool can tell the difference between "Pink Floyd" and "Ani DiFranco".  They're both names.  One is a band name, one is an artist's name.

However, see:
http://yabb.jriver.com/interact/index.php?topic=92092.0
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/
Pages: [1]   Go Up