Using File Names to Make a Database


This page presents a blisteringly powerful way of organizing files. By embedding data in a file's name according to a naming convention, you can ask your favorite file manager to show data about the file—such as a movie's year and director, a book's author and number of pages, or any kind of value or "flag": Is the status "on hold"? Is the game compatible with macs?

Viewing movie metadata in file manager

The example in the image above shows comedy movie files with columns for the title, director, duration in minutes, year, IMDB rating, country code, language and subtitle format.

With such a layout, you could click the "Year" column to sort the movies by year of release, or group the movies by country code.

Several key features may not jump out at you:

✽ the metadata displays correctly even though the files are in different formats (avi, mkv).
✽ the files themselves do not contain the metadata shown in the columns.
✽ the file names are in the column at the very left (which has been minimized and only shows an arrow).
✽ The "tt" column contains the movie's IMDB code. If you press Ctrl + I, the IMDB page for the movie opens!
✽ More columns exist but aren't shown. For instance you can embed the name of a key actor, an award won by the movie, a yes/no flag indicating whether you've seen it—basically any data about movies you feel like keeping in your "database".

This technique is useful to manage many kinds of files, from an architect's CAD files to a dentist's collection of comics.

This page contains a complete walk-through of the technique, as well as two fully fleshed-out packages (one to manage movie files, the other to manage books or comics files), and instructions to adapt these packages to any kind of files.


(direct link)
Jumping Points
For easy navigation, here are some jumping points to various sections on the page:

Options to Store Metadata
Using File Names to Carry Metadata
An All-Purpose File Naming Convention
A Movie Database based on File Names
A Comics (or Books) Database based on File Names
Bulk Metadata Work on Your Files
Editing and Defining Fields
Adapting the Scripts for Other Kinds of Files


(direct link)

Options to Store Metadata

Before diving in, let's step back for a moment to look at various ways data about files (or metadata) can be stored.

Metadata stored by the operating system
A file's very name is metadata. So is its creation date. These fields (and others, such as permissions) are stored by the operating system.

Metadata embedded inside files
By now, most people who have used mp3 and other music files are familiar with the idea of embedding metadata inside each track (for instance, the artist and album names). This is useful not only in a music playback device and in music management software, but also if you use a file manager that lets you see the various metadata fields in columns. In such a file manager, you can click on the Album column to sort your music, or on the Height column to sort your images.

This works well for music files and jpg images, where standards describe how the metadata should be embedded in the files.

When there is no embedding standard
Then there are file formats where metadata can be embedded (usually using an xml sub-file to describe the main file), such as movies in the mkv container, or comics in cbz archives.

Often, different programs come up with different ideas about how to embed the metadata—there is no standard. This means that if you use a dedicated program to manage your collection, you get married to that program. If you move your files to a different collection manager, your metadata may not display properly.

Embedding by mixing metadata with content
For a text file with a txt extension, you don't have the option of embedding an xml file in the original file.

But you can include a descriptive section at the beginning of the file. If that description follows a standard (for instance, it always begins with Start_Description and ends with End_Description), then you could conceivably write a script that peeks inside your text files and extracts the metadata, when it exists.

This technique would be pushing the semantic boundaries for metadata, since the metadata would be part of the text container (the file's data) and will interfere with the "actual data" if you view files in regular text editors.

When embedding is not an option
For certain file types,
✽ you may not have the option or the tools to embed metadata in the file (for instance, an exe file)
✽ you may reject the option of mixing the metadata with the content because it interferes with the file
✽ you may hesitate to adopt existing metadata standards because they only apply to one of several types of files that you want to manage together.

In such cases, you'll probably want to turn to using a file naming convention.


(direct link)

Using File Names to Carry Metadata

File naming conventions are nothing new. Everyone uses them. For instance, if you add a prefix to your Word files to indicate the date (as in 150101-New-Year-letter-to-Joan.doc), you have a file naming convention.

This particular file name carries three pieces of information about the file:
✽ A date (150101),
✽ A description of the content (a New Year letter),
✽ A recipient (Joan).

By using a file naming convention, file names can be harnessed to carry useful information about files. For instance, this would be a reasonable name for a movie:
6.6 The Valet (2006).avi
It obeys the following convention: Rating Movie_Name (Year).extension

This particular convention allows you to sort movies by their rating, since the 6.6 is at the beginning of the file's name. Conversely, if your movie file names looked like
2006 The Valet (6.6).avi,
you could sort them by year. But you can't do both.

Or can you?...

With the right tools, you can. This is what this page is all about.

The right tool will extract the metadata out of any file naming convention you choose, allowing you to see it into columns, sort files, group them and so on.


(direct link)

An All-Purpose File Naming Convention

There is nothing wrong with the two file naming conventions we just saw, of which the two following names are examples:
6.6 The Valet (2006).avi
2006 The Valet (6.6).avi

In fact, you can easily edit the movie database script lower on the page to extract the year and rating from either format. For instance, to extract the year from the second format, you would replace the year's field definition with this simple one (please don't worry about understanding the details):

'year' : { pattern: /^\d{4}/ }

These two naming conventions are fine, but they are not designed to carry a lot of metadata. What if you want to add the director's name? The number of minutes? The country?

There are many possible solutions to this problem. Any of them can be used by the movie database and the books and comics database scripts lower on the page. The particular convention I've implemented is easy to extend. Here is how it works:

✽ A simple file might look like this:
The Stranger [y=1942_auth=Albert Camus].epub
✽ The metadata lives [in the brackets]
✽ The metadata is a series of key=value pairs (for instance y=1942 is one such pair, auth=Albert Camus is another)
✽ The key=value pairs are separated by the underscore character _
✽ In each file name, you only use the fields you want. You do not have to write empty fields.
✽ The order of the key=value pairs doesn't matter. This means you could have named the file
The Stranger [auth=Albert Camus_y=1942].epub
and the metadata would still work. You don't have to remember a complex order.
✽ Acceptable values for each key are specified in the script file. Here the year can be specified with either y=, yr= or year=. This allows your memory to be a bit fuzzy on occasion.

(direct link)
Aren't Long File Names a Problem?
With this system, you can end up with long file names. For instance, I wouldn't consider this especially long. Don't be scared!

The Valet (La Doublure) [y=2006_ir=6.6_cc=FR_lng=fre_sub=en-srt_min=82_tt=tt0449851_dir=Francis Veber_act=Gad Elmaleh].avi

Will this trash my display?
Look again at the image from the top of the page:

Viewing movie metadata in file manager

The giant file name above corresponds to the one highlighted on the image!

You don't need to worry about long file names cluttering your screen, because the entire file name is not meant to be shown in the file manager. Instead, we'll create layouts that show exactly the columns we want to see.

However, there is a limit to how much data you can have in the file name. And that limit depends not only on the length of the file's name, but also of that of its path on the operating system.

Windows has a 260-character limit for sum of the lengths of a file's name and of its path from the root (for instance, for C:\python\boa.txt, that total would be 17 characters). 260 characters is enough to do a lot of things, but bear in mind that if you store files deep in the folder hierarchy, you will have fewer characters left.

To put things in perspective, the movie file name above that looked extremely long only takes up 63 characters. This means 197 characters are left to specify the folder's path. That's a lot.

(direct link)
What tool should you use with these files?
This page is published on my "fan site" about Directory Opus (in my view the finest productivity improvement software there is), so it's no surprise that I had Opus in mind for these filename-based databases. But after using the scripts for a while, and while writing the page, I wondered what other programs can use this technique. After all, extracting fields via regex is not rocket science.

This research let me to the site of XYplorer (another well-regarded file manager), where I learned that it too can create custom columns by extracting fields from file names. The feature is called scriptable columns. I don't have XYplorer, but I suspect the column definitions in this page's scripts would be easy to adapt.

If you know of other tools that can readily extract data from file names, please let me know by using the comment form.


(direct link)
Installing Scripts
Once you're downloaded one of the scripts below, I suggest you first install it without making any edits to check that you can make it work "out of the box".

✽ In the Opus Preferences panel, type script in the bottom left filter. This opens the Scripts page.
✽ Drag the script and drop it into the panel.
✽ Make sure the checkbox is checked.
✽ Click Apply and OK
✽ Name one file according to the naming convention.
✽ Right-click a column header. In the Script menu, pick a column that corresponds to a field you've set in the file name.
✽ The column will be added in the rightmost position of the lister. Scroll to the right to make sure it displays properly.


Editing Scripts
If things work correctly, at this stage you can customize the script if it doesn't fit your need. To do so, edit it in a text editor. The section about defining fields has examples of how to add and edit fields. And the section about adapting scripts explains how to create a copy of the script to use with other kinds of files.

Once you've edited the script, save it. Once again, open the the Scripts page of the Preferences panel. Drag and drop the new script, and choose the Replace option. Make sure the checkbox is checked. Apply... You should be good to go.

Editing is usually a multi-pass process. I recommend leaving the Scripts page of the Preferences panel open when you edit scripts so you don't have to open it with every little change. You can drop the script repeatedly on that page without closing it.


(direct link)
Generic Script: RegexColumnsReloaded
To implement all the features on this page, you can if you wish start from a generic regex columns script I've made called RegexColumnsReloaded. This script allows you to add any kind of regex column—you don't have to follow a naming convention.

But I've also adapted RegexColumnsReloaded into two sample scripts to showcase how one might craft a full solution that creates columns based on naming convention. These are the Movie FileName Database Script and the Books & Comics FileName Database script that we're about to see. So you can also start from these.

Bear in mind that you can add any kind of regex column to the Movies and Books scripts. They are loaded with columns designed to parse file names into specific columns, but they are also full-featured regex column scripts, since they are based on the same core: RegexColumnsReloaded.

With that, let's jump right into the Movie FileName Database Script.


(direct link)

A Movie Database based on File Names

So maybe you're pleased with yourself because over the years you've ripped the DVDs you've acquired so that if a disk stops working, your movie won't be lost. Over the years, you've used different software to rip, so that now your movie collection is a mélange of divx, avi and mkv files —not to forget the occasional VOB files.

You'd love to tag your movies in order to keep track of directors and other items of interest, but how do you get the metadata for all these files to display consistently? Some collection managers want to transcode all your files into one central format—heart attack! (By the way, the last time I looked at iTunes, that's what it did to music. But don't let me get started on my Apple rant.) A gentler solution would be to repack all your files into an mkv container and go with someone's embedded xml solution.

Our solution here just relies on naming the movie files according to a simple and flexible convention.

Viewing movie metadata in file manager


Naming Convention
The naming convention follows the Key = Value format presented above. As a reminder,

✽ A simple file might look like this:
The Valet [y=2006_ir=6.6].avi
✽ The order of the key=value pairs doesn't matter. This means you could name the file
The Valet [ir=6.6_y=2006].avi
and the metadata would still work. You don't have to remember a complex order.
✽ There may be several acceptable values for each key. For instance, the year can be specified with either y=, yr= or year=. This allows your memory to be a bit fuzzy.

Here are the fields currently available through the script. Remember that you only use the ones you want.

ColumnValid KeysNotes
Title(automatic)Everything to the left of the [brackets]
Actoract, actor, actressWho featured in it?
AgainagainWould you like to watch it again some day?
Awardawd, awardawards won. e.g. Oscar, Palme d'Or
Cameracam, camera, cinCinematographer e.g. Robby Müller
Colorcol, colorIs the movie in color? B&W?
CCcc, countryCountry code: Which country is the movie from? E.g. US, FR, RU
Critiquecrit, critiqueCritique, e.g. "great", "meh"
Directordir, director Who directed it?
IRirimdb rating, e.g. 7.5
Langlang, lngLanguage, e.g. "eng"
Minmin, dur, durationDuration in minutes, e.g. 120
Numnum, nb, noNumber in the series, e.g. 01
Qualq, qual, qualityQuality, e.g. "blurry"
Ratingrat, ratingYour rating
Scripttxt, text, scr, scriptWho wrote the script?
Seenseen, snHave you watched it? E.g. yes, T, false, N, 1, 0...
Seriesser, seriese.g. "Three Colors"
Subssb, sub, subs,
sbs, subtitle,
subtitles
subtitles, e.g. en, de, fr
Tagstag, tagstags, e.g. "comedy,romance"
TTttimdb code, e.g. tt0478331
Yeary, yr, yearWhat year was it released?
Namelen(automatic)The length of the file name.
Charsleft(automatic)The number of characters still available to use in the path + file name.


Special Fields
In the table above, you may notice that for three fields, you don't need to type in a key=value pair in the brackets.

Title: the title is assumed to be everything to the left of the brackets (minus any spaces immediately preceding the left bracket.)
Namelen: this is the number of characters in the file name. Note that Opus also provides a column for the length of path + file name (right-click column header, General / Path length)
Charsleft: this is the number of chars still available to use in the path + file name. Windows imposes a 260 maximum length for the Path + File name, so this column can be handy if you are getting close.

Bear in mind that you are not limited to columns that fit the file naming convention:
✽ The script is based on the RegexColumnsReloaded script, so it can handle any regex columns
✽ The script has a special space where you can define special columns whose value can be the product of any kind of code. You don't have to use regex everywhere.


(direct link)
The Magical IMDB Field
One field you're going to love is the tt field. It contains IMDB's unique identifier for a movie title. For instance, if you open this link for the movie The Valet, you will see that the url is
http://www.imdb.com/title/tt0449851
The identifier is the section that starts with tt. To insert it into your file metadata, you would add tt=tt0449851/

What does that do?

(direct link)
The Magical Movie Button
movie button on file manager When you install the script, you can also install a Movie button on your toolbars. This button has several functions:

✽ If your file contains the tt field, the button can open the imdb page for the movie (shortcut: Ctrl + I).
✽ If your file does not contain the tt field, it can search Google for the imdb page for the movie (shortcut: Ctrl + I). Usually the first result is the one. You can then double-click the tt field right from Google results and add it to your file.
✽ The button can toggle "Movie View". This assumes you've set up a view (in Opus terms, "folder format") to take advantage of the metadata: we'll look at this next.

The button toggles full-row selection mode, which enables you to easily select your file even when the file name column is minimized.

(direct link)
Setting Up a "Movie View"
It's convenient to be able to switch from your usual layout to one you've set up especially for movies. In fact, the movie button is ready to toggle a Movie view.

There are several ways to set up a Movie view. You can ask Opus to automatically switch to that view when more than a given proportion of files in a folder are movie files. Or you can toggle the view manually. I do the second, but we'll talk about both. First, let's cover the differences.

✽ For a view that turns on automatically, you'll set up the view in Prefs / Folders / Folder Formats / Content Type Formats. You'll be able to edit the existing Movies view there. You'll also need to check the "Enable Folder Content Type detection" box in Prefs / File Displays / Find-As-You-Type.
✽ For a view that you turn on manually, you'll set up the view in nearly the same menu: in Prefs / Folders / Folder Formats / Favorite Formats.
✽ The movie button is set up for the second option. If you'd like the toggle menu item to work with the first option, you'll have to change the part of the button code that says Set Format "Movies" to Set CONTENTFORMAT Movies.

To get you started, here is the Movies view that works for me at the moment. Note that the filename field is quite narrow. That's because we don't need to see the whole file name. Since the Movie button activates full-row selection, we can still easily select the files.

movies view in Directory Opus


(direct link)
Downloading and Installing the Script
The latest version of the script will always live on this thread on the Directory Opus forum.

One of many advantages of hosting the latest file on the Opus forum is that the script can be updated using tbone's ScriptWizard script.

✽ Follow the installation instructions given earlier.
✽ Don't forget to install the movie button!


(direct link)

A Comics (or Books) Database based on File Names

So you have a collection of electronic books. PDF files can contain some metadata. So can mobi files (Kindle) and epub files (generic ebook format). If your book collection contains comics, you can embed metadata in your cbz or cbr files.

So, all is well, right?

No, in fact this situation is a mess:

✽ To manage your regular ebook files, you'll probably use Calibre, which will let you edit the metadata.
✽ To manage your comics, you may decide to use a program such as ComicRack.
✽ These programs may not support some fields you want to embed.
✽ You may soon find yourself with multiple copies of files on your computer—some in the collection managers' folders with metadata included, then original versions without metadata.
✽ In your file manager, you can't select files based on their metadata, because the metadata is only available in the collection managers (Calibre and ComicRack).

What to do?

Viewing book and comics metadata in file manager

The solution in the image above lets you use the same format for all kinds of ebook files (see the pdf, cbr and cbz in the image). It may not fit everyone's needs: the metadata you enter will appear in the file manager, but it won't transfer automatically to collection managers such as Calibre and ComicRack. I use it and I like it. It's up to you.

Naming Convention
The naming convention follows the Key = Value format presented above. As a reminder,

✽ A simple file might look like this:
The Hunting Party [art=Enki Bilal_txt=Pierre Christin_yr=1983].cbz
✽ The order of the key=value pairs doesn't matter. This means you could name the file
The Hunting Party [yr=1983_txt=Pierre Christin_art=Enki Bilal].cbz
and the metadata would still work. You don't have to remember a complex order.
✽ There may be several acceptable values for each key. For instance, the year can be specified with either y=, yr= or year=. This allows your memory to be a bit fuzzy.

Here are the fields currently available through the script. Remember that you only use the ones you want.

ColumnValid KeysNotes
Title(automatic)Everything to the left of the [brackets]
AgainagainWould you like to read it again some day?
Artistart, artist Who drew the book?
Authorauth, authorUse when the same person is the writer and artist
Awardawd, awardawards won. e.g. Eisner, Fauve d'Or
Colcol, colorIs the book in color? Grayscale? Duotone?
Countrycc, countryWhich country is the book from? E.g. US, FR, DE
Critcrit, critiqueCritique, e.g. "great", "meh"
Eded, editionEdition, e.g. "3rd"
Langlang, lngLanguage, e.g. "eng"
Listlist, lstDoes it feature on a list?
Numnum, nb, noNumber in the series, e.g. 01
Publisherpub, publisherPublisher
Qq, qual, qualityQuality, e.g. "blurry scan"
Pgspg, pgs, page, pagesNumber of pages
Ratingrat, ratingYour rating
ReadreadHave you read it? E.g. yes, T, false, N, 1, 0...
Seriesser, seriese.g. "Tintin"
Tagstag, tagstags, e.g. "scifi,romance"
Writertxt, text, wr, writerWho wrote the text?
Yeary, yr, yearWhat year was it published?
Namelen(automatic)The length of the file name.
Charsleft(automatic)The number of characters still available to use in the path + file name.


You may notice that for three fields, you don't need to type in a key=value pair in the brackets. These are the same fields explained in the special fields section of the Movie Script.

Bear in mind that you are not limited to columns that fit the file naming convention:
✽ The script is based on the RegexColumnsReloaded script, so it can handle any regex columns
✽ The script has a special space where you can define special columns whose value can be the product of any kind of code. You don't have to use regex everywhere.


(direct link)
Books + Comics View Button
The download page for the script includes a button. The button can toggle "Comics view". This assumes you've set up a view (in Opus terms, "folder format") to take advantage of the metadata: we'll look at this next.

The button also toggles full-row selection mode, which enables you to easily select your file even when the file name column is minimized.

(direct link)
Setting Up a "Books or Comics View"
It's convenient to be able to switch from your usual layout to one you've set up especially for books or comics. I'll refer you to the instructions to set up the Movie view.

To get you started, here is the Books and Comics view that works for me at the moment.

comics view in Directory Opus


(direct link)
Downloading and Installing the Script
The latest version of the script will always live on this thread on the Directory Opus forum.

One of many advantages of hosting the latest file on the Opus forum is that the script can be updated using tbone's ScriptWizard script.

Follow the installation instructions given earlier.




(direct link)

Bulk Metadata Operations on Your Files

If you already follow a naming convention and would like to switch to the one presented here, it's a renaming problem, which is not a big problem.

Those who know regex will write one or several rename presets. If that's not you, you can either:
learn regular expressions (a very worthwhile investment),
✽ use one of the scripts on the Opus forum such as MrC's Dynamic Renamer,
✽ Use a standalone renaming tool. The best I've seen by far is Gillmeister's Rename Expert.

(direct link)
Adding One Field to Many Files
Let's say you have already tagged your files and you'd now like to add the field cc=US to many files. These are the steps:

✽ Select the files to rename. (The filter bar is your friend. So is checkbox mode.)
✽ Open the Opus renamer.
✽ Switch to regular expressions mode.
✽ In the Old Name field, paste ^([^\]]+)(\].*)
✽ In the New Name field, paste \1_cc=US\2
✽ Make sure to save the preset.

If you want to add another token, open the preset and edit it on the fly.


(direct link)

Editing and Defining Fields

Adding columns to the "database" is easy. Open the script file and look for the section labelled "DEFINE YOUR FIELDS (i.e. COLUMNS) HERE".

Column Definitions
If you scroll past the "manual", you will see that a column definition looks like this:

'Column Name' : { (properties) },

For column definitions that parse file names into Opus columns based on regular expressions, inside the curly braces, you have one or more properties. At the very minimum, you have the pattern property, which contains the regular expression to be used. For instance, this is all it takes to define a simple Title column:

'Title' : { pattern: /^(?:[^ ]| (?!\[))*/ },

You can add more properties which I will explain in a moment, but for the time being you may be shocked by the /^(?:[^ ]| (?!\[))*/ in the pattern. This is a text matching pattern known as a regular expression. If you want to craft columns that don't fit the format of this script, it will greatly help if you learn some regex (an investment that will pay you back a thousand times over). But you don't need to know regex to add a basic key=value column because these all look about the same.

(direct link)
Adding a simple key=value column
Let's say you are using our naming convention to set column in the file names by using bracket definitions such as [year=1985_author=Bell]

To create a column called Status, so that you can add values such as stat=On Hold in your file name, this is all you have to add to the script:

'Status' : { pattern: /\[(?:[^\]]+_)?stat=([^\]_]+)/i, group: 1 },

Drop it in, update the script in Preferences / Scripts, and you can start using your column!

If you want a column with a name other than Status, and a key name other than stat, just copy the exact code above and edit the two words!

A simple key alias
Here is a refinement if you want the option to use two aliases when setting the Status column in the file name: stat and status. Notice that these two are made of the same prefix stat, followed by an optional us. Your column definition becomes:

'Status' : { pattern: /\[(?:[^\]]+_)?stat(?:us)?=([^\]_]+)/i, group: 1 },

Notice the bold sections? I'm sure you can see how to adapt this to a column whose aliases are either col or color.

You can make more complex aliases, but this is as far as I will take you if you don't know regex. You can now skip to the Cool Properties section to refine your columns.

For those who do know regex, there will be some great tricks lower down.

(direct link)
Files with a Different Naming Convention need Different Regular Expressions
If your files follow a different naming convention from the one in the Movie Database and the Comics Database, you will need different regex.

If your regex is not strong, post your problem on the RegexColumnsReloaded script thread on the Opus forum. If I don't see it, someone else probably will.

Another place to ask is StackOverflow. Make sure to describe your data, your desired output and what you have already tried, as people on the site want to feel they are helping others learn how to fish rather than doing their homework. Site etiquette suggests you upvote all helpful answers and add a checkmark to accept the one that helped you the most.

(direct link)
The Cool Properties Available in a Column Definition
Our first column, Title, had a single property: pattern. Our second column, Status, had two properties: pattern and group. We'll now talk about the cool properties you can add to a column definition.

You could, for instance, add a width property to the Title column. In fact, this is what we have in both scripts:

'Title' : { pattern: /^(?:[^ ]| (?!\[))*/, width: 30 },

This tells Opus that by default, unless specified otherwise, the Title column will be created with a large-enough span to accommodate 30 characters.

Here are all the properties we can use. If something doesn't make sense to you, you are probably not meant to use it.

pattern: the regex pattern.

group: the regex capture Group that will set the column value. By default, this is zero (the whole match).

target: the specific property of the file that will be the subject of the regex. By default, this is the file's name stem without the extension (or name_stem). Other possible values are name (the file name including the extension), ext (the extension), realpath (the file's full path including the file name itself), and more.

width: the column's default width, in number of characters.

justify: specifies whether the column should be justified left (the default), right, center or path.

sort: when set to DESC, if you click on the column to sort it, it will first sort in descending order (helpful for dates and ratings, for instance.)

type: by default Opus will display your columns as plain text, but columns can be set to other types too: number, double, size, zip, percent, graph, igraph (try this!), date, time, datetime, stars.

infotiponly: when it is set to true, the column will only display in the "info tips" that pop up when you hover over files. These info tips are defined per file type in the file type editor.

subjectPrefix: a string that gets preppended to the target string to form the regex subject. This facility enables you to transform the text in the regex without writing a special column case in the code.

returnTheFirstNonEmptyGroup: a Boolean (true/false) that indicates we should return the first group that is not-empty. Takes precedence over "group".

fileOnly: a Boolean (true/false) that restrict the column to files (don't display for folders).

dirOnly: a Boolean (true/false) that restrict the column to folders (don't display for files).

fullCupValue: using this value, convert the column to a scale of 5 or 100 depending on whether the type is stars (5) or percent, graph and igraph (100).


(direct link)
Special Columns
The focus of this script is to use regular expressions in column definitions to parse the file name into columns. However, sometimes you may want to display a column that cannot be obtained with regex, such as, for instance, the total number of vowels in a file name.

That's no problem: the script has built-in room for special columns. There are two steps.

First, define a column. You can leave the definition empty, as below, or set the width property and others we've already seen. For instance, for a column about the file name's length:

'Name Length' : { },

Second, in the function OnRegexColumn, find the section labelled "First, handle special NON-REGEX Columns". There, test for your column name, and compute or set the column's value. For instance:

// Is this the name length column? if (colName == 'Name Length') { // Set the column's value ColumnData.value = ColumnData.item.name.length.toString(); }

That's all!

If you have several special columns, remember to use else if on the second one.


(direct link)
Trick: inspecting the file properties reported by Opus
Most of your regular expressions will run against the file's name stem (the default mode). You know what the name looks like, so writing these kinds or expressions should be straightforward if you know regex.

But the column definition format in the scripts allows you to specify different targets for the regex, such as modify (the file's last modification date), or realpath (the file's path).

Unless you know exactly what these properties look like, it will be hard to manipulate them or search them using regex. Before you try doing so, I therefore recommend you set up a temporary column to display the property.

To do so, we use a regex pattern that matches the whole property. For instance, to inspect the modify date, we just add this column:

'Modify' : { pattern: /.*/, target: "modify" }

A Modify column is now available in your lister, allowing you to inspect what the values look like.

(direct link)
Advanced Trick: Boolean Columns using Regex (no ifs!)
As we discussed, you can add special columns that don't fit our standard column definition format by creating special if cases later in the code.

You would think that in order to display a yes / no value based on the presence of text in a column, you would indeed need to write such a special if case. However, with the awesome trick I'm about to show you, you can create yes / no columns using the regex column definitions you've already seen. The trick uses the technique I fleshed out in my article about matching line numbers on my regex site.

To make this work, we only need to add one property to our column definition: subjectPrefix. This subjectPrefix field contains a text prefix that will be preppended to the subject text against which the regex tries to match. For instance, if we set the subjectPrefix to the string 0 and the target property is somefile, the regex will run against 0somefile.

The trick is that during the regex match, we use the extra characters we've preppended to the subject. Here are some examples.

To display Y when the file's is at a depth level of three or more in the file hierarchy (counting the root of a drive as level 0), we could use this simple column:

'3-deep' : { pattern: /^(Y)(?:[^\\]*\\){4}/, target: "realpath", group: 1, subjectPrefix: "Y" }


The column is included in the Movies script so you can try it. What happens here?

✽ The capture group that will be shown in the 3-deep column is Group 1.
✽ Now notice that our target is realpath, which is the file's full path (for instance, C:\Python\readme.txt).
✽ To this target, subjectPrefix preppends Y. The subject that the regex runs against therefore becomes YC:\Python\readme.txt
✽ The regex captures the initial "Y" to Group 1 (which sets the value of the column when there is a match), then proceeds to match four backslashes
✽ This means that when four backslashes are found (when we are at depth level 3 or more), the column will display the content of Group 1, which is Y.

Here is a variation that not only displays Y when the depth level is 3 or more, but which displays N when it isn't. It is also included in the Movies script.

'3-deep YN' : { pattern: /^Y(?=(?:[^\\]*\\){4})|N/, target: "realpath", subjectPrefix: "YN" }

Here,
subjectPrefix preppends YN to the target, so that the subject the regex runs against becomes YNC:\Python\readme.txt
✽ We use an alternate pattern on the right side of the | in order to match the N when the left side fails.
✽ Instead of displaying Group 1, we display the whole match because Group 1 cannot be recycled to match either the Y or the N. This forces us to use a lookahead.

Expanding the if-less regex trick to columns with sets of 3 or more possible values
Note that the trick above doesn't have to produce Boolean columns. For instance, the following column displays num if the file stem is entirely numeric, CAPS if it is entirely in upper case, low if it is entirely in lower case.

The main difference is that we add the property returnTheFirstNonEmptyGroup, which tells Opus to set the column to the value of the first capture group that is not empty. (If JavaScript regex supported \K or lookbehinds, we could write the regex without this new property.)

'NumCapsLow' : { pattern: /^(num)CAPSlow\d+$|^num(CAPS)low[A-Z]+$|^numCAPS(low)[a-z]+$/, subjectPrefix: "numCAPSlow", returnTheFirstNonEmptyGroup: true }


Using the same idea, we can display three values depending on the path's depth—"0-2", "3-5" or "6+":

'pathdepth' : { pattern: /^0-2(3-5)6\+(?:[^\\]*\\){4,6}[^\\]*$|^0-23-5(6\+)(?:[^\\]*\\){7}|(0-2)/, target: "realpath", subjectPrefix: "0-23-56+", returnTheFirstNonEmptyGroup: true }


The possibilities are endless!


(direct link)

Adapting the Scripts for Other Kinds of Files

If you'd like to adapt the Movie FileName Database Script for another kind of files, these are the steps. That's exactly what I did to create the Books & Comics FileName Database script. We'll assume the theme of your new collection is CAD files.

✽ Copy the the script file (its extension is js.txt) to a new file

✽ Rename the new file to something like CADdatabase.js.txt

✽ Open the file in a text editor.

✽ Find the initData.name line and change it to initData.name = "CAD Columns";

✽ Find the ColumnPrefix line and change it to ColumnPrefix = "CAD_";

✽ Edit the columns as explained in the Defining Fields section

✽ Install the script as explained in the Installing Scripts section

In an Opus lister, you can now right-click the column header and select your columns under Scripts / CAD Columns. And under Prefs / Folders / Folder Formats, you can select your columns while defining formats.

Well, that's all I have to say about the topic of "file name databases" for the time being! This page was a lot of work and I'm sure there are typos here and there. If you see any, please write to me using the form at the bottom. I'd also be interested (really!) to hear your ideas about this topic.

Smiles,

Andy


Make a Donation

Did this website just save you a trip to the bookstore?

Producing and maintaining this kind of content is a huge amount of work. Please make a donation to support this site.   

next
 Master your Opus preferences




1-1 of 1 Threads
Kyle – USA
March 06, 2017 - 14:31
Subject: Removing the file name length limit

You have been able to remove the file name length limit in Windows 10 since the anniversary update. It seems to work fine in DO12. Https://www.howtogeek.com/266621/how-to-make-windows-10-accept-file-paths-over-260-characters/


Leave a Comment






All comments are moderated.
Link spammers, this won't work for you.

Kindly note that I will not reply to general Opus tech support questions (but feel free to ask about issues directly related to my toolbars).


To prevent automatic spam, we require that you type the two words below before you submit your comment.