Script to create column from text in ()

I haven't been around for a while, so I'm excited (and surprised) to see all this talk about custom columns. It could solve a sorting issue I've been battling for quite a long time.

Can someone help me with a script that will pull any text inside () at the end of a filename, and pop it into a sort-able column?

Example Filename (New Column).ext

Thanks!

In VBScript:

[code]option explicit

Dim re
Set re = Nothing

' Called by Directory Opus to initialize the script
Function OnInit(initData)
initData.name = "Brackets Column"
initData.desc = "Extract stuff in brackets at the end of filenames"
initData.copyright = "(C) 2015 Leo Davidson"
initData.version = "1.0"
initData.default_enable = true

Dim col

Set col = initData.AddColumn
col.name = "Brackets"
col.method = "OnBrackets"
col.label = "Brackets"
col.justify = "left"
col.autogroup = true

End Function

' Implement the Brackets column
Function OnBrackets(scriptColData)
If (scriptColData.col <> "Brackets") Then
Exit Function
End If
If (re Is Nothing) Then
' re made the first time the column is requested, then cached.
Set re = new RegExp
re.pattern = "^.*(([^()]+))$"
End If
If (re.Test(scriptColData.item.name_stem)) Then
scriptColData.value = re.Replace(scriptColData.item.name_stem, "$1")
End If
End Function[/code]

All the boilerplate (almost everything except the body of "OnBrackets") can be made for you via File > New Script in the Preferences dialog, when on the Toolbars / Scripts page.

Outstanding, this works great, thanks leo!

Thanks Leo, this works real nice!
MY folders don't have the info in the parentheses in the end, but actually it's right before the end. Is it possible to modify the script so that it works for that setup as well?
For example, my folders look like this: "Double Indemnity (1944) [720p]". Perhaps the script can create a column for both, what's in parentheses and what's in the brackets...
Looking fw to your thoughts!
AR

Change:

re.pattern = "^.*(([^()]+))$"

to:

re.pattern = "^.(([^()]+))[b].[/b]$"

to remove the requirement that the brackets are at the end.

With a slightly different regex you can extract from [ ] instead of ( ). Here's a basic one:

re.pattern = "^.[(.+)].$"

(That may not do what you want if there's more than one [ and/or ] in the name.)

You might also want to look at these existing script add-ins, some of which let you configure multiple columns without editing script code (but still requiring you to understand regexps):

[ul][li]RegExColumns V2[/li]
[li]Columns: No Brackets, Highest Number, Filtered Name[/li]
[li]Custom Column - Custom Text V1.2[/li]
[li]RegexColumnsReloaded (easily add regex columns or any kind of column)[/li][/ul]

Hi Leo,
I just tried it and it works beautifully! Thank you so much!

Is there some place I can add:
re.pattern = "^.[(.+)].$"
So that it it extracts what's in both the parentheses AND in the Brackets and creates a column for each at the same time?

Sorry, I just realized you posted some threads, I will look at those...
Thanks!

[quote="leo"]Change:

re.pattern = "^.*(([^()]+))$"

to:

re.pattern = "^.(([^()]+))[b].[/b]$"

to remove the requirement that the brackets are at the end.

With a slightly different regex you can extract from [ ] instead of ( ). Here's a basic one:

re.pattern = "^.[(.+)].$"

(That may not do what you want if there's more than one [ and/or ] in the name.)[/quote]

I'm struggling to adapt these two expressions for use in JS. Can anyone help?

Never mind I'm just running multiple versions of leo's VBS script to make multiple columns and it's working nicely :slight_smile: