INTERACT FORUM

Please login or register.

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

Author Topic: Creating Expressions for File Renaming  (Read 2331 times)

EpF

  • Citizen of the Universe
  • *****
  • Posts: 649
Creating Expressions for File Renaming
« on: January 25, 2012, 08:03:07 pm »

Mr.C suggested that I post about this, and after first demurring, I thought 'why not?', especially because the process becomes more complex as I address more Media Types and I could use some more help!

The first thing I did was try to create an expression(wiki link) that would move all files based on their Media Type, but I've realised that the expression would become massive an unwieldy if I ever wanted to change anything, so I'm doing it on a per-media-type basis now.

The way I'm implementing it is to create a series of calculated fields(wiki link) which I then string together in a final expression which allows me to make changes to things in easy-to-manage chunks.

To get started, I use this expression to determine the base folder for each media type (I'm using quote markup because it's easier to read):
Quote
ifelse(isequal([media type],Audio),
      D:\AUDIO\,
   isequal([media type],Video),
      \\ShackServer\VIDEO\,
   isequal([media type],Image),
      if(isequal([sub-type],Film Still),
         \\ShackServer\VIDEO\Film\,
      D:\IMAGE\),
   1,
D:\DOCUMENTS\MISCELLANEOUS\)
I should point out that I use some alternative takes on some standard MC fields, like [sub-type] instead of 'media sub type', for some added flexibility.

Anyway, this expression (used in a field called 'Move Base Path'), goes like this, in more understandable terms:

If Audio > D:\AUDIO\
otherwise
If Video > \\ShackServer\VIDEO\
otherwise
If Image & If Sub Type = Film Still > \\ShackServer\VIDEO\Film\
 If Not Film Still > D:\IMAGE\
otherwise
>D:\DOCUMENTS\MISCELLANEOUS\

The reason for the little 'shimmy' for images of sub-type 'Film Still' is that I want to keep film-still images in their own folder with the video files.

For people less familiar with what I'm trying to do here: Basically all you want to get out at the end of this expression is a string of text that you could, if you wanted, paste into a Windows Explorer address bar to be taken straight to the Folder. The expression doesn't actually do any renaming, it's just a way of telling MC how you want things renamed when you use the Rename/Move/Copy feature.

With that in mind, all this expression does is create one of the following possible strings to place at the beginning of the renaming string, depending on the media type:
D:\AUDIO\,
\\ShackServer\VIDEO\,
\\ShackServer\VIDEO\Film,
D:\IMAGE\
D:\DOCUMENTS\MISCELLANEOUS\

On to the next part of the string... This is where it gets complicated, and I have spent hours staring at expressions wondering where the hell they've gone wrong, but there are a few things which help me:
  • 1 - I use NotePad++ as a text editor in Windows, and I've created a language definition which presents the expressions in a much more readable way
  • 2 - I make much use of the ability to create temporary expression columns in the file list for testing expressions
  • 3 - I create a calculated field called '_test', so it shows up at the top of the field list for quick access
(If anyone wants a copy of that NotePad++ file to import, you can get it here: link)


AUDIO FOLDERS

So, the next part is to tell MC where to put things like TV Shows, Audiobooks, Music and Photographs. I'll deal with Audio first, and I have to thank Mr.C for his help on this one. First, here's the expression:
Quote
if(isequal([sub-type],Audiobook),
   AUDIOBOOK\[Artist]\ifelse(!isempty([Series Title]),[Series Title]\[Book #] - )[Album],
if(isempty([sub-type]),
   MUSIC\if(isequal([source],archive,8),ARCHIVE,ACQUIRED)\[AssortedOrArtist]\[Album],
MISC\[sub-type]\ifelse(!isempty([Artist]),[artist]\,!isempty([Album]),[album]\,1,)))
And here's the more readable version:
If Audiobook > AUDIOBOOK\Artist\
 If Series Title > Series Title\Book # - Album
  otherwise
  > Album
otherwise
If Sub Type Empty > MUSIC\
 If Source = archive > ARCHIVE\Artist\Album
  otherwise
  > ACQUIRED\Artist\Album
otherwise
> MISC\Sub Type\Artist\Album (only if 'artist' and/or 'album' are not empty)

And here's what it the rename string looks like now:
Audiobooks in a series;D:\AUDIO\AUDIOBOOKS\Artist\Series Title\Book # - Album\
Audiobooks NOT in a series;D:\AUDIO\AUDIOBOOKS\Artist\Album\
Music ripped from my CD collection;D:\AUDIO\MUSIC\ARCHIVE\Artist\Album\
Other music;D:\AUDIO\MUSIC\ACQUIRED\Artist\Album\
Any other type of audio;D:\AUDIO\MISC\Sub Type\Artist\Album\ - unless Artist or Album is empty, in which case either or both are omitted

If you're wondering what 'AssortedOrArtist' is, it's another calculated field  to emulate the 'Use "Assorted" instead of "Artist" on multi-artist files' feature in rename/copy/move. The expression looks like this:
Quote
if(IsEmpty([artist]),
   if(IsEmpty([album]),
      Unknown Artist\Unknown Album,
   Unknown Artist\[album]),
If(IsEqual(Mid([album type]),M),
   Assorted,
[artist])\if(IsEmpty([album]),
      Unknown Album,
   [album]))
The reason for this is that that feature won't work when using an expression in the 'Rule' box. I'm sure it could be streamlined too, but it serves for the moment.

And that's it for Audio files - apart from the filenames themselves, of course :) . So far only directories have been addressed.


VIDEO FOLDERS

My Video requirements are quite similar to my Audio ones. My basic setup is:
\Film\
\TV Show\
\Misc\Sub Type\

So my Video Folder expression looks a lot like the Audio one:
Quote
if(isequal([sub-type],Film),
   FILM\ifelse(!isempty([series title]),[series title]\[episode #] - )[name]\,
if(isequal([sub-type],TV Show),
   TV SHOW\[tv show]\ifelse(!isempty([series #]),[series #]\,1,),
MISC\[sub-type]\ifelse(!isempty([Artist]),[artist]\,!isempty([Album]),[album]\,1,)))
The basic logic is:
If it's a film, do A
 If it's a TV Show, do B
Otherwise do C

Part 'A', as for Audio, inserts "Series Title\Episode # - " into the string only if Series Title is not empty (for film series like Star Wars, The Bourne Trilogy, etc.). Otherwise it creates a folder with the film's name
Part 'B' only creates a Series # folder if Series # has a value (for series like 'Cosmos' or 'Planet Earth' or 'Firefly' which don't have more than one series)
Part 'C' is exactly the same as for Audio.

That's it for Videos. The result is this:
Films in a series;\\ShackServer\VIDEO\FILM\Series Title\Episode # - Name\
Films NOT in a series;\\ShackServer\VIDEO\FILM\Name\
TV Shows\\ShackServer\VIDEO\TV SHOW\TV Show\Series #\
Any other type of video;\\ShackServer\VIDEO\MISC\Sub Type\Artist\Album\ - unless Artist or Album is empty, in which case either or both are omitted

Images and more are dealt with two posts down: link

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Creating Expressions for File Renaming
« Reply #1 on: January 25, 2012, 09:43:26 pm »

Thanks for doing this, EpF!

I was planning to write this up back in the day, but never got back to it after I hit the problem with the file path delimiter and then JRiver added the profiles feature to Rename, Move, & Copy so my need for this wasn't as dire.  In any case, if you are trying this out, this thread might be a useful read.

Also, thanks a LOT for this:

  • 1 - I use NotePad++ as a text editor in Windows, and I've created a language definition which presents the expressions in a much more readable way
(If anyone wants a copy of that NotePad++ file to import, you can get it here: link)

I'm installing that definition now.
Logged
"Some cultures are defined by their relationship to cheese."

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

666JackTheKnife666

  • World Citizen
  • ***
  • Posts: 150
Re: Creating Expressions for File Renaming
« Reply #2 on: January 26, 2012, 01:16:20 am »

big ditto on the  NotePad++ profile. Thank you!!

Your post has given me some interesting idea's to play with, Good Job!
Logged

EpF

  • Citizen of the Universe
  • *****
  • Posts: 649
Re: Creating Expressions for File Renaming
« Reply #3 on: January 26, 2012, 10:38:19 am »

Continuing on from the first post...

IMAGE FOLDERS UPDATED #2

This was the big one for me, firstly because I'm using both Sub Type and Genre as base organising folders - but not for all images - and secondly because I have more basic sub-types which I want to use for organising. This is the current version, which works except for Film Stills, which only partly works. It has been updated with Mr.C's regex suggestion from below:
Quote
ifelse(isequal([sub-type],Film Still),
         [album]\Still\,
      isequal([sub-type],Scan,8),
         SCANS\,
      isequal([sub-type],Graphic Design),
         GRAPHIC DESIGN\[genre]\,
      isequal([sub-type],Photograph),
         PHOTOGRAPHY\if(regex(:[artist]:, /#^:(unknown|none)?:$#/),
                      {Unknown Artists},
                   [artist]\)
                   if(regex(:[camera]:, /#^:(unknown|none)?:$#/),
                      ,
                   [camera]\)
                   if(regex(:[country]:, /#^:(unknown|none)?:$#/),
                      ,
                   [country]\)
                   if(regex(:[region]:, /#^:(unknown|none)?:$#/),
                      ,
                   [region]\),
      isempty([sub-type]),
         ,
      1,
         MISC\if(ifelse(!isequal([sub-type],CGI),
                        ifelse(!isequal([sub-type],Handmade),
                           [sub-type]\)),
                        [sub-type]\,
                     [sub-type]\ifelse(!isempty([genre]),[genre]\)))

Note that there is some illegal formatting here, which I've ignored for clarity: The carriage return and whitespace after [artist]\), [camera]\), [country]\) and [region]\) in the PHOTOGRAPHY section need to be eliminated because there is no comma at the end of the line.

Here's a more readable version:
If Film Still > Album\Still\
otherwise
If Sub Type contains Scan > SCANS\
otherwise
If Graphic Design > GRAPHIC DESIGN\Genre\
otherwise
If Photograph > PHOTOGRAPHY\Artist\Camera\Country\Region\ - unless Artist, Camera, Country and/or Region is empty or contains 'none' or 'unknown', in which case they are skipped apart from Artist, which will be {Unknown Artists}
otherwise
If Sub Type empty > {blank}
otherwise
> MISC\
 If Sub Type is neither CGI nor Handmade > Sub Type\
  otherwise
  > Sub Type\Genre - unless Genre is empty, in which case it is skipped

Here are the basics of what this expression does when combined with the Base Path expression:
Film Stills;\\ShackServer\VIDEO\FILM\Album\Stills\ - I have to tag Film Still [album] tags with the same name as my Film videos' [name] tags in order to get them in the same folder as the films. This is not optimal for films in a series, because I have to remember to use the episode number in the album name and I have to get film [series title] in there too, so actually this only works for films not in a series. Something I need to think more about.
Scans;D:\IMAGE\SCANS\
Object Scans;D:\IMAGE\SCANS\
Graphic Design;D:\IMAGE\GRAPHIC DESIGN\Genre
Photgraphs;D:\IMAGE\PHOTOGRAPHY\Artist\Camera\Country\Region\
CGIs;D:\IMAGE\MISC\CGI\Genre\
Handmade;D:\IMAGE\MISC\Handmade\Genre\
Images with no Sub Type;D:\IMAGE\
Any other images;D:\IMAGE\MISC\Sub Type\

One thing that is worth pointing out is the use of nested 'ifElse' functions under 'MISC' - one of the basic differences between 'If' and 'IfElse' is:
'If' uses 3 parameters: Value-to-test, what-to-do-for-TRUE, what-to-do-for-FALSE
'IfElse' uses 2 parameters: Value-to-test, what-to-do-for-TRUE

As a result 'IfElse' is simpler to use when you only need to do something if the test-value is true. IfElse's main feature is that you can string a series of test/result pairs together - but you don't have to.


THE FINAL FIELD

This is the expression I use in the final [Move All Dir] calculated field:
Quote
ifelse(isequal([media type],Audio),
      [move audio dir],
   isequal([media type],Video),
      [move video dir],
   isequal([media type],Image),
      [move image dir],
   1,
[file type]\)
This uses the previous calculated fields to sort all Media into their correct folders once they've been tagged properly. Data goes to D:\DOCUMENTS\MICSCELLANEOUS\File Type\

The last thing to do is to use the expression replace([move base path],_,\) in the Base Path input in Rename/Move/Copy and replace([move all dir],_,\) in the Rule input. This is because Rename/Move/Copy will replace backslashes with underscores when using fields in renaming expressions. The Replace() expression reverses that - as I said in the first post, all you want at the end of this process is a string of text that conforms to the Windows path you want to use for the different files.



You can then save this as a preset in Rename/Move/Copy and it will always be available.

I will update this post as I progress - I intend to cover filenames as well. Any suggestions and corrections would be very welcome.

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Creating Expressions for File Renaming
« Reply #4 on: January 26, 2012, 11:33:23 am »

Here's a little trick you can use to shorten and simplify your multiple tests for the empty value, "unknown", and "none".  Instead of:

ifelse(
  isempty([artist]),
      {Unknown Artists}\,
  isequal([artist],unknown,8),
      {Unknown Artists}\,
  isequal([artist],none,8),
      {Unknown Artists}\,
  1,
       [artist]\)

use the following:

  if(regex(:[artist]:, /#^:(unknown|none)?:$#/), {Unknown Artists}, [artist])

This will compare empty, unknown or none in one shot.

Why does [artist] have : : surrounding it?  Because MC doesn't handle testing an empty string correctly:

  regex([artist], /#^$#/)

should match when [artist] is empty, but it does not.  So, instead, we force artist to always look like :<artist>:, and test for two consecutive :: characters as the empty string.  The expression above looks for a : character, followed optionally by either unknown or none, followed by a final : character.
Logged
The opinions I express represent my own folly.

EpF

  • Citizen of the Universe
  • *****
  • Posts: 649
Re: Creating Expressions for File Renaming
« Reply #5 on: January 26, 2012, 12:27:20 pm »

Nice one Mr.C! Will update soon - does regex searching cause any extra processing load, do you know?

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Creating Expressions for File Renaming
« Reply #6 on: January 26, 2012, 12:35:34 pm »

regex() is more intensive, but for your infrequently used fields like this, it should not be noticeable.
Logged
The opinions I express represent my own folly.

EpF

  • Citizen of the Universe
  • *****
  • Posts: 649
Re: Creating Expressions for File Renaming
« Reply #7 on: January 27, 2012, 11:06:15 am »

I've updated the second post with Mr.C's regex (thanks!) and added a section on the final renaming field.

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Creating Expressions for File Renaming
« Reply #8 on: January 27, 2012, 12:14:30 pm »

Maybe some more shortcuts...

When you have empty positive clauses in your If() statements, use IfElse with an inverted (negated) test:

Quote
if(regex(:[camera]:, /#^:(unknown|none)?:$#/),
                      ,
                   [camera]\)
                   if(regex(:[country]:, /#^:(unknown|none)?:$#/),
                      ,
                   [country]\)
                   if(regex(:[region]:, /#^:(unknown|none)?:$#/),
                      ,
                   [region]\),

You can negate the test with ! and simplify the formatting so that you don't have dangling empty commas, and alignment helps see the important values:

Quote
ifelse(!regex(:[camera]:,  /#^:(unknown|none)?:$#/),    [camera]\)
ifelse(!regex(:[country]:,  /#^:(unknown|none)?:$#/),    [country]\)
ifelse(!regex(:[region]:,   /#^:(unknown|none)?:$#/),     [region]\),
Logged
The opinions I express represent my own folly.

lise

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 946
Re: Creating Expressions for File Renaming
« Reply #9 on: April 04, 2012, 10:26:11 am »

Hmmm. I'm trying to replicate this but it isn't working.  I'm just working on the basic path rule.

Yours:
Quote
ifelse(isequal([media type],Audio),
      D:\AUDIO\,
   isequal([media type],Video),
      \\ShackServer\VIDEO\,
   isequal([media type],Image),
      if(isequal([sub-type],Film Still),
         \\ShackServer\VIDEO\Film\,
      D:\IMAGE\),
   1,
D:\DOCUMENTS\MISCELLANEOUS\)

mine
Quote
ifelse(
isequal([media type],Audio), \\Lizzy\Media (M)\Music\,
  isequal([media type],video), \\Lizzy\Media (M)\Video\),1,
C:\User\Lise\Documents\)

Saved as a calculated field called my base path.
There does seem to be an extra ). Not sure why it is there at the end of video\) as well as being the final character in the entire expression. I've tried eliminating each one, one at a time, but it doesn't help.

When renaming, I put the following in the Base Path field of the rename dialog box as per your instructions:
Quote
replace([my base path],_,\)

Basically the result is that it completely ignores any change in directory for data files (the default), and for audio or video I get this:

Quote
from:  \\MAL\users\Public\ to \\Lizzy\Media (M)\Music\,0,\Lizzy\Media (M)\Video,1,C_\Users\Lise\Dcouments\
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: Creating Expressions for File Renaming
« Reply #10 on: April 04, 2012, 10:56:37 am »

You need to escape parens -- those around your M -- when used in the expression language.  There was also an extra closing paren at the first isequal line.  Here's the cleaned up version:

ifelse(
   isequal([media type],Audio), \\Lizzy\Media /(M/)\Music\,
   isequal([media type],video), \\Lizzy\Media /(M/)\Video\,
   1,                                    C:\User\Lise\Documents\
)
Logged
The opinions I express represent my own folly.

lise

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 946
Re: Creating Expressions for File Renaming
« Reply #11 on: April 04, 2012, 11:24:12 am »

MrC, thank you so much.
I didn't bother trying that before because the original \\ as in \\Lizzy didn't need them so I figured if \ doesn't need a /, then perhaps ( doesn't need one anymore either. Lesson learned. Thanks for taking the time. I appreciate it very much.
Logged
A wise man once said don't count your years, but make your years count. Or was it beers?
Pages: [1]   Go Up