INTERACT FORUM

Please login or register.

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

Author Topic: Auto import rules: assign folder name to a field.  (Read 2230 times)

lise

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 946
Auto import rules: assign folder name to a field.
« on: July 27, 2012, 11:48:40 am »

For the past 3 hours I've been looking for how to assign folder names to various fields in MC with auto-import.
I've read many complicated suggestions in various threads and have tried modifying the codes to suit my needs but I've failed. I thought perhaps if there was a simple step-by-step instruction it could help me and others.

Say you have files named as follows:

c:\User\Me\Documents\Cooking\Appetizers\Aioli.txt
c:\User\Me\Documents\Woodworking\Sharpening\Sharpen with sandpaper.txt

You set auto-import to look at this path: C:\User\Me\Documents

Question 1.
Now you want to assign a rule that says: Assign the next folder's name (Cooking or Woodworking) as the "Genre" tag.  What is the rule? 

Question 2.
And say you want to assign a tag, say "subgenre" to the next folder after that (Appetizers, Sharpening).  What is the rule to do only this?

Question 3. 
What is the rule if you want to do both operations in one shot? (i.e. assign the first folder after Documents to the "Genre" field and the next folder to the "Subgenre" field?)
Logged
A wise man once said don't count your years, but make your years count. Or was it beers?

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Auto import rules: assign folder name to a field.
« Reply #1 on: July 27, 2012, 11:58:27 am »

There is currently no mechanism to assign two fields at once, so two rules are necessary.

Probably the easiest way to grab the 4th and 5th directory components is with listitem()

Genre:
   listitem([filename], 4, \)

Subgenre:
   listitem([filename], 5, \)
Logged
The opinions I express represent my own folly.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Auto import rules: assign folder name to a field.
« Reply #2 on: July 27, 2012, 12:09:12 pm »

Probably the easiest way to grab the 4th and 5th directory components is with listitem()

Genre:
   listitem([filename], 4, \)

Subgenre:
   listitem([filename], 5, \)

That's exactly how I do it.  It works great if your path is rigidly defined.
Logged
"Some cultures are defined by their relationship to cheese."

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

lise

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 946
Re: Auto import rules: assign folder name to a field.
« Reply #3 on: July 27, 2012, 12:22:54 pm »

Great. Thanks. That's nice and easy.  Now to complicate things a little.

What if you want to assign a default genre of "personal" to all your subfolders except for Cooking and Woodworking?

c:\User\Me\Documents\finances\taxes\2012
c:\User\Me\Documents\Gadgets\
c:\User\Me\Documents\Cooking\Appetizers\
c:\User\Me\Documents\Woodworking\Sharpening\

So all folders under Documents get assigned the genre "Personal" except for Cooking and Woodworking, which get the subfolder name as the genre?
Logged
A wise man once said don't count your years, but make your years count. Or was it beers?

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Auto import rules: assign folder name to a field.
« Reply #4 on: July 27, 2012, 12:42:07 pm »

I would use the built-in OR'ing of regular expressions in this case:

if(regex([filename],
   /#c:\\User\\Me\\Documents\\(Cooking|Woodworking)\\#/), [R1], Personal)

[R1] will be either Cooking, or Woodworking, and when not one of those, the RE will fail and the If() will output Personal.  This way, you can add additional alternatives easily.
Logged
The opinions I express represent my own folly.

lise

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 946
Re: Auto import rules: assign folder name to a field.
« Reply #5 on: July 27, 2012, 01:03:28 pm »

Thanks MrC.

Just to be clear, in Field I select Genre and in Value I put:

Code: [Select]
if(regex([filename], /#c:\\User\\Me\\Documents\\(Cooking|Woodworking)\\#/), [R1], Personal)
This assigns the "Personal" genre to all files except those under Cooking and Woodworking which get assigned the Cooking genre and the Woodworking genre respectively. Yes?

So that if I wanted to also assign the Film genre to all files in c:\User\Me\Documents\Film, I would modify your expression by simply adding |Film next to Woodworking as follows:

Code: [Select]
if(regex([filename], /#c:\\User\\Me\\Documents\\(Cooking|Woodworking|Film)\\#/), [R1], Personal)
Is that correct?

(is there a space between the very first comma and the "/#c"?)
Logged
A wise man once said don't count your years, but make your years count. Or was it beers?

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Auto import rules: assign folder name to a field.
« Reply #6 on: July 27, 2012, 01:19:26 pm »

You've got it.  The RE matches the path name alternatives, and remembers the portion you want for Genre.  If the RE is successful, the remembered component is used in the TRUE part of the If().  When not matched, If()'s FALSE portion is output.

Whitespace after commas in function arguments is ignored.  You can add/remove to suite your needs.
Logged
The opinions I express represent my own folly.

lise

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 946
Re: Auto import rules: assign folder name to a field.
« Reply #7 on: July 27, 2012, 01:50:28 pm »

That is absolutely friggin fantastic!  What a great way to achieve what I want (and no doubt it will be beneficial to others as well). Thanks so much for sharing.

The only other thing I can think of at the moment would be the following. I'm repeating myself here just to be very clear for others needing this help.

c:\User\Me\Documents\Cooking\Appetizers\Aioli.txt
c:\User\Me\Documents\Woodworking\Sharpening\Sharpen with sandpaper.txt

Root import folder set to c:\User\Me\Documents

1st subfolder becomes the "genre" field using listitem([filename], 4, \) as the value  (e.g., Cooking, Woodworking)
2nd subfolder becomes the "subgenre" field using listitem([filename], 5, \) as the value (e.g., Appetizers, Sharpening).

What if there is no 2nd folder? Must we first construct an if clause such that if the 2nd folder after the root exists use that as the subgenre, otherwise don't assign a subgenre?
Logged
A wise man once said don't count your years, but make your years count. Or was it beers?

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Auto import rules: assign folder name to a field.
« Reply #8 on: July 27, 2012, 09:42:35 pm »

With your root as c:\User\Me\Documents, you can test that [filename (path)] contains at least 6 components (using 0-based counting):

   ifelse(listitem([filename (path)], 5, \), listitem([filename (path)], 5, \))

c:\User\Me\Documents\GENRE\SUBGENRE
 0    1    2        3             4         5

If there is no 6th component, then the return is unassigned so nothing would be tagged.
Logged
The opinions I express represent my own folly.

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42381
  • Shoes gone again!
Re: Auto import rules: assign folder name to a field.
« Reply #9 on: July 27, 2012, 10:17:06 pm »

You might like FileFolder(...) as well.

It uses a reverse index, so 0 is the last folder, 1 is the parent, 2 is the grandparent, and so on.
Logged
Matt Ashland, JRiver Media Center
Pages: [1]   Go Up