INTERACT FORUM

Please login or register.

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

Author Topic: Expression to get bottom folder in a pathname?  (Read 5883 times)

ThoBar

  • Citizen of the Universe
  • *****
  • Posts: 992
  • Was confishy
Expression to get bottom folder in a pathname?
« on: April 11, 2007, 09:33:41 am »

Simply:

Return "folder3" from "x:\folder1\folder2\folder3\file.ext"

Obviously we can get the volume name, "x:\folder1\folder2\folder3", "file", "file.ext"

I thought it may be possible using isequal() or mid() or somesuch, however my poor little brain isn't capable of deriving an appropriate statement with MC's expression language (if actually possible).

Why? I want to be able to view a group of files in a folder in a view scheme with the format of:  [Libraryfield1]/[Libraryfield2]/folder3  (where folder3 is indicated as above).

Weird? Perhaps. But still, would be nice if I could do it :D

Cheers.
C.

[edited for stupid exception-to-the-rule spelling error]  >:(
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #1 on: April 11, 2007, 09:54:05 am »

I'd be interested in a solution to *precisely* this problem as well :D
Logged

KingSparta

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 20063
Re: Expression to get bottom folder in a pathname?
« Reply #2 on: April 11, 2007, 02:02:32 pm »

does the expression list include "InStr" if so then it may be allowed.

if so you could use (maybe Left$, Right$ Or Mid$) in conjunction with "InStr" To Search the Field For "\" starting at pos 4 (Past the first "\" Like In "C:\")

Code: [Select]
Returns a Variant (Long) specifying the position of the first occurrence of one string within another.

Syntax

InStr([start, ]string1, string2[, compare])

The InStr function syntax has thesearguments:

Part Description
start Optional.Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start containsNull, an error occurs. The start argument is required if compare is specified.
string1 Required.String expression being searched.
string2 Required. String expression sought.
compare Optional. Specifies the type ofstring comparison. If compare is Null, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.


Settings

The compare argument settings are:

Constant Value Description
vbUseCompareOption -1 Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.
vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.


Return Values

If InStr returns
string1 is zero-length 0
string1 is Null Null
string2 is zero-length start
string2 is Null Null
string2 is not found 0
string2 is found within string1  Position at which match is found
start > string2 0


Remarks

The InStrB function is used with byte data contained in a string. Instead of returning the character position of the first occurrence of one string within another, InStrB returns the byte position.


Logged
Retired Military, Airborne, Air Assault, And Flight Wings.
Model Trains, Internet, Ham Radio, Music
https://MyAAGrapevines.com
https://centercitybbs.com
Fayetteville, NC, USA

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #3 on: April 11, 2007, 02:09:33 pm »

Don't see it here

i think it would be great if they provided a FileFolder() expr, to complement the already present File Name, Volume & Path exprs.
Logged

lalittle

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3964
Re: Expression to get bottom folder in a pathname?
« Reply #4 on: April 11, 2007, 02:30:24 pm »

I actually looked into doing this a while back as well.  I have a display view for some documents where I'd like to display and sort one of the panes using JUST the final subfolder in the path.  I'd also like to have the main name pane sort by this filed.

I agree with hit_ny that this field would be a great addition to MC's list of fields, complementing the existing Filname(name) and Filename(path) fields.  I was thinking that it should follow the pattern of the other fields -- i.e. something like "Filename(subfolder)."

Thanks,

Larry
Logged

ThoBar

  • Citizen of the Universe
  • *****
  • Posts: 992
  • Was confishy
Re: Expression to get bottom folder in a pathname?
« Reply #5 on: April 11, 2007, 10:28:50 pm »

Quote
I agree with hit_ny that this field would be a great addition to MC's list of fields, complementing the existing Filname(name) and Filename(path) fields.  I was thinking that it should follow the pattern of the other fields -- i.e. something like "Filename(subfolder)."

I also agree with this...

Perhaps filename(foldercount) could return how many folders there are (simply count the "\" is the easiest)...
and filename(subfolder,n) could return the n'th folder name, be it first, last or somewhere in between.

C.
Logged

lalittle

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3964
Re: Expression to get bottom folder in a pathname?
« Reply #6 on: April 11, 2007, 10:39:30 pm »

I also agree with this...

Perhaps filename(foldercount) could return how many folders there are (simply count the "\" is the easiest)...
and filename(subfolder,n) could return the n'th folder name, be it first, last or somewhere in between.

C.

If it didn't know how many folders were in the path, however, how would it know which one was last?

Larry
Logged

ThoBar

  • Citizen of the Universe
  • *****
  • Posts: 992
  • Was confishy
Re: Expression to get bottom folder in a pathname?
« Reply #7 on: April 11, 2007, 11:04:10 pm »

Quote
If it didn't know how many folders were in the path, however, how would it know which one was last?

Something along the lines of:

filename(subfolder,filename(foldercount))

so if you had "x:\f1\f2\f3\file.ext" it and wanted the last folder, the filename(foldercount) portion would return 3 (3 folders), and so the subfolder request would look like: filename(subfolder,3) and return "f3"

(it may look like filename(subfolder,filename(foldercount)-1) depending on the count starting at 0 or 1)

C.
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #8 on: April 11, 2007, 11:41:47 pm »

Thats interesting, but i think the default behaviour should be to return the last folder, *without* having to specify how far along the path, so it could be used in general situations, where last folder or subfolder is required from any filepath.

No objections to allowing additional options to specify which folder to return.

The reason i specified expression rather than fields, is if its implemented as a field, then a re-import (or Update from tags) of the whole library would be required.

An expression however could be dropped in as a column to a viewscheme/smartlist and would work immediately, an expression would also allow more options as to which folder to return :)
Logged

lalittle

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3964
Re: Expression to get bottom folder in a pathname?
« Reply #9 on: April 12, 2007, 02:14:28 am »

Thats interesting, but i think the default behaviour should be to return the last folder, *without* having to specify how far along the path, so it could be used in general situations, where last folder or subfolder is required from any filepath.

No objections to allowing additional options to specify which folder to return.

I agree.

Quote
The reason i specified expression rather than fields, is if its implemented as a field, then a re-import (or Update from tags) of the whole library would be required.

I'm a bit confused here.  Filename(name) and Filename(path) are called Standard Library "Fields" in the MC options -- are these actually expressions?  I ask since these don't require an "update" like you refer to. 

Thanks for clarification,

Larry
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #10 on: April 12, 2007, 05:16:20 am »

No, they are library fields.

I was talking about experience with a new field called Compressoin that was added i think to 11.1. At the time if you upgraded to this version, that had this new field, it would not be filled for any of the existing library items. (which is the correct behaviour)

Only future imports would have it filled. The only way to populate the compression field for existing library items was to either update library from tags or a re-import, both of which force MC to read the file again.

So if they create a new field Filename(folder), my feeling is that it will not be filled when you upgrade to the version that has it, the only way would be to re-read the file again, and this would mean reading all the other tags in the file and replacing  the content of library fields with their file tag counterpart.

If your file tags are not in sync  with the library,  (i keep field updates in the  database only) then all field updates made to the library for the tags that are also present in that file will be lost !!..unless they do it in such a way that only the new field is populated and ignores the other fields, which sounds like more work and isn't the default way of doing it,  which is read all tags.

This is why maybe an expression might be easier to implement and could even have more options with it :)

They could of course do it both ways, offer a folder expression as well as a folder field, i would just ignore the field, and use the expression in this case.
Logged

lalittle

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3964
Re: Expression to get bottom folder in a pathname?
« Reply #11 on: April 12, 2007, 05:26:52 am »

I was talking about experience with a new field called Compressoin that was added i think to 11.1. At the time if you upgraded to this version, that had this new field, it would not be filled for any of the existing library items. (which is the correct behaviour)

I'm not sure this situation would apply to the file "path" and "name" fields.  When you highlight a file that is not in the library, these fields are ALWAYS correct -- i.e. you don't need to "update" anything to get them to display correctly.  If this is the case, it seems like a "Filename(lastfolder)" field could automatically display the correct information without the "update" procedure.

If anyone reading this specifically knows how this works, please post here.

Thanks,

Larry
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #12 on: April 12, 2007, 05:42:46 am »

Quote
When you highlight a file that is not in the library, these fields are ALWAYS correct -- i.e. you don't need to "update" anything to get them to display correctly.  If this is the case, it seems like a "Filename(lastfolder)" field could automatically display the correct information without the "update" procedure.

Thats because you added to the library and MC just read all its attributes & tags.

I was referring to how already existing items in the library would be able to use the new library field.

My understanding of fields in MC is that they are filled at import or just read if added to PN.

If so, then the only way to populate for new library fields is to force a re-read.
Logged

lalittle

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3964
Re: Expression to get bottom folder in a pathname?
« Reply #13 on: April 12, 2007, 05:45:03 am »

Thats because you added to the library and MC just read all its attributes & tags.

I was actually talking about files that are not added to the library -- i.e. just highlighting a file via drives and devices.  What you're saying, however, may still apply.

Larry
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #14 on: April 12, 2007, 06:43:46 am »

Exactly, so they have brown tune icons insted of blue. These are re-read when you add.
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #15 on: April 18, 2007, 10:08:46 am »

ahem
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #16 on: May 10, 2007, 03:57:13 pm »

oops
Logged

hit_ny

  • Citizen of the Universe
  • *****
  • Posts: 3310
  • nothing more to say...
Re: Expression to get bottom folder in a pathname?
« Reply #17 on: May 31, 2007, 03:55:01 am »

again
Logged

Tanoshimi

  • Junior Woodchuck
  • **
  • Posts: 57
Re: (RESOLVED) Expression to get bottom folder in a pathname?
« Reply #18 on: March 13, 2012, 03:02:59 pm »

Ok, so after struggling with it for a while, I got this one solved.  I now have a Field named [Folder].  It's a calculated field, with a value of:

MC 16 = Regex(RemoveRight([Filename (path)],1) , /#(.*\\)(.*)#/, 0)[R2]

MC 17 = RemoveLeft(Regex(RemoveRight([Filename (path)],1) , /#(.*\\)(.*)#/, 0)[R2],1)

That takes my filename (path) from C:\My Video\Instructional\Aikido\ and sets [Folder] = Aikido.

Good Luck!
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: Expression to get bottom folder in a pathname?
« Reply #19 on: March 13, 2012, 03:29:21 pm »

You resurrected a really old thread, but it seems like this should be easier.

So in a coming build:
NEW: Added expression FileFolder(...) to get the folder of a file: FileFolder(string Filename = [Filename, 0], int LevelsUp = 0).

For example:
FileFolder() > Folder for current file
FileFolder([Filename, 0], 1) > Parent folder for current file
FileFolder(C:\Grandparent\Parent\Folder\File.dat, 2) > Grandparent
Logged
Matt Ashland, JRiver Media Center

Tanoshimi

  • Junior Woodchuck
  • **
  • Posts: 57
Re: Expression to get bottom folder in a pathname?
« Reply #20 on: March 13, 2012, 03:48:10 pm »

Not sure if this helps or not, but we also made a list of folders via:

Replace([Filename (path)],\,;)&datatype={list}
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: (RESOLVED) Expression to get bottom folder in a pathname?
« Reply #21 on: March 13, 2012, 05:20:45 pm »

Ok, so after struggling with it for a while, I got this one solved.  I now have a Field named [Folder].  It's a calculated field, with a value of:

MC 16 = Regex(RemoveRight([Filename (path)],1) , /#(.*\\)(.*)#/, 0)[R2]

MC 17 = RemoveLeft(Regex(RemoveRight([Filename (path)],1) , /#(.*\\)(.*)#/, 0)[R2],1)

That takes my filename (path) from C:\My Video\Instructional\Aikido\ and sets [Folder] = Aikido.

FYI: simplified, letting the RE do it all:

   Regex([Filename (path)], /#\\([^\\]+)\\$#/, 1)

@Matt - consider expanding the functionality of ListItem() using a negative index.  This gives you not only the same thing as FileFolder(), but also the ability to grab any list item from the end (with arbitrary separator).
Logged
The opinions I express represent my own folly.

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42372
  • Shoes gone again!
Re: (RESOLVED) Expression to get bottom folder in a pathname?
« Reply #22 on: March 13, 2012, 05:32:52 pm »

@Matt - consider expanding the functionality of ListItem() using a negative index.  This gives you not only the same thing as FileFolder(), but also the ability to grab any list item from the end (with arbitrary separator).

I thought about that, but there are two things I don't like:

1) Out-of-bounds index values should return empty, not some new magical thing
2) Since 0 is reserved, you would have to use 1-based numbering in reverse (ie. start at -1, -2, -3, etc.)

So I think a new function ListItemReverse(...) or a final parameter like "BOOL bReverse" would be more appropriate.
Logged
Matt Ashland, JRiver Media Center

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression to get bottom folder in a pathname?
« Reply #23 on: March 13, 2012, 05:37:16 pm »

Great!
Logged
The opinions I express represent my own folly.

Tanoshimi

  • Junior Woodchuck
  • **
  • Posts: 57
Re: Expression to get bottom folder in a pathname?
« Reply #24 on: March 14, 2012, 05:25:46 pm »

How about Replace([Filename (path)],"\",";").  that turns it into a list of folders.  That's what I'm using on my sytem now.
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: Expression to get bottom folder in a pathname?
« Reply #25 on: March 14, 2012, 05:52:18 pm »

As an FYI....

The expression:

   Replace([Filename (path)], \, ;)&datatype=[list]

will present all sub-folders for all files in a list.  For paths such as:

   C:\Media\Music\Hooverphonic\A New Stereophonic Sound Spectacular\*.flac

your get all the folders below in the list:

   C:
   Media
   Music
   Hooverphonic
   A New Stereophonic Sound Spectacular

so it is slightly different than the OP's request, but still useful.
 
Note: you don't want double quotes.
Logged
The opinions I express represent my own folly.
Pages: [1]   Go Up