Conditional File Naming

Hi folks! I’m new to DEVONthink and working to understand what I can do with SmartRules. My first simple project is vexing me. I’m hoping someone can point me in the right direction.

Basically, if a filename starts with the date pattern “YYYY-MM-DD FILENAME”, I want to import it with the name intact.

If the filename does not start with the date pattern “YYYY-MM-DD” I want to rename the file to using the creation date to place the date at the front of the filename. “YYYY-MM-DD FILENAME”.

I’m sure I’m missing something simple. Thanks for any guidance.

1 Like

It took A LOT of digging to finally figure it out. The answer was simpler than I realized.

1 Like

I have a more extensive set of file naming rules; also tag assignment
My solution is an Applescript, triggered by the import smart rule

1 Like

Welcome @roba1701
You should not have criteria-less smart rules. At a minimum, Kind is Any Document is advisable. You should strive to be as specific as possible with automations.

Basically, if a filename starts with the date pattern “YYYY-MM-DD FILENAME”, I want to import it with the name intact.

This doesn’t need to be considered as the only function of the rule is to change the name when it doesn’t match. File with names beginning with four numbers-two number-two numbers will not be matched and import as-is.

If the filename does not start with the date pattern “YYYY-MM-DD” I want to rename the file to using the creation date to place the date at the front of the filename. “YYYY-MM-DD FILENAME”.

1 Like

Thanks. I have lots to learn yet about AppleScript. :face_with_monocle:

Thank you. I will give that a whirl. Are there any resources with examples of SmartRules use? I’m trying hard to come up to speed.

A few things…
These forums have plenty of examples. There’s also the Scripts > More Scripts > Smart Rules where you can install extra smart rules. Just duplicate them and dissect / edit them .

Some stuff on our blog, including this…

PS: There’s no need to rush into things, especially if you’re new to DEVONthink. Go at a comfortable pace and use the app the way you need to use it now. Automations should come after you’ve established actual processes you can do manually.

2 Likes

Great. Thanks so much.

1 Like

FYI. If I run it on demand, it changes an existing filename that already has a YYYY-MM-DD set. The match is not working?

It did what you told it to do… on demand.

But if the rule states not to process a file if it starts with a sortable date, why do all my files with sortable dates show up when the rule is highlighted? It doesn’t make sense. Shouldn’t it only select file names that don’t start with a sortable date? What am I missing here? I was a DBA and I’m confused. LOL

I’m not looking at your computer so I can’t see what you’re talking about.
Screen captures or a screencast would be helpful. If you don’t want to share it publicly, open a support ticket and attach them.

2 Likes

Done. Thank you, Jim.

1 Like

Problem solved. Needed to negate the pattern with a “!” in front.

e.g. name matches ![0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]

Indeed and it’s a matches not a begins with that supports wildcards and operators, e.g., [0-9]. That was my incorrect reply earlier.

1 Like

I think I’m trying to achieve the same goal of the OP, but so far I’m not succeeding…

If I use:
Name matches ![0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]

it returns filenames which do not contain a date (in any position)

I would like to filter in a way that the rule only displays filenames where the date does not appear at the beginning of the string

For example, the rule should return:

  • css-kranken-versicherung-ag 2023-06-27.pdf

but not:

  • 2023-06-27 css-kranken-versicherung-ag.pdf

(which is already correctly named)

Can you help ?

Only matches conditions support operators and wildcards. But adding another condition like Name does not begin with 202 might be an option.

What I can propose is a script that adds a tag to all PDF files whose names don’t begin with a date:

(() => {
  const app = Application('DEVONthink');
  const records = app.databases.contents();
  records.filter(r => r.recordType() === 'PDF document' 
    && (!r.name().match(/^\d{4}-\d\d-\d\d/))).forEach(r => {
      r.tags = r.tags().concat('Fix Filename')
  })
})()

This script is tested; all modifications mentioned below are not.
If you want to handle all documents, not only PDFs, remove the part
r.recordType() === 'PDF document' &&

Or, if you want to change the file name for those files that contain a date already, but at the wrong position, replace r.tags = r.tags().concate('Fix Filename') with

const dateMatch = r.name().match(/(\d{4}-\d\d-\d\d)/);
if (dateMatch) {
  const date = dateMatch[1];
  r.name = `${date} ${r.name().replace(date, '')}`;
} else {
  r.tags = r.tags().concate('Fix Filename')
}
1 Like

Thank you @cgrunenberg

So if I understand correctly, for the moment it is not possible to achieve the desired parsing with matches only (due to limitations in the complexity of regex you can provide there - I guess ?)

I tried to add a second condition as you suggested and it does the trick → thanks for your support

Thank you @chrillek

The approach proposed by Christian works for me, however your suggestion is also interesting and I will play with it to “practice” with scripting :slight_smile:

However - if I correctly understand - it involves two steps, i.e.:

  1. run the script to add the tag to the matching files and
  2. use the smart rule to look for the tag and apply the renaming

Did I get it right ?