Struggling with regex file rename despite following instructions in other posts

Hey there. I’m trying to bulk rename a bunch of files with a format like
Filename (more information) 123456789 2024-07-22.pdf
to move the date to the beginning like
2024-07-22 Filename (more information) 123456789.pdf

I’ve tried both the “rename using regex” script and the batch processing tool, using the following regex.
Source pattern/ Scan Name > Regular Expression:
(.*)(\d{4}-\d{2}-\d{2})(\.pdf)
Destination pattern/ Change Name:
\2 \1\3

Nothing happens after hitting ok on the source pattern - no change to the filename of the selected file.

I know that there are several flavors of regex, and you can choose them in regex101 to see how your code runs. It seems like the backslash replacement method mentioned a lot in the forums works when Flavor is set to PCRE, but PCRE2 prefers the use of
$2 $1$3
for the replacement string. regex101.com seems to indicate that both of these set ups work - the results in the substitution window are what I expect - depending on the flavor you choose. BLUEFROG I’ve seen your mention of a preference for regexr.com. That site seems to indicate that you need to use the dollar sign version of the code. Unfortunately, using this in the batch processing tool actually renames the file to
$2 $1$3.pdf

I’m running Devonthink 3 Version 3.9.6, which “check for updates” says is the latest version, on Sonoma 14.5.

What am I missing about how this works?

In this case, regular expressions aren’t even needed. You can do this via Tools > Batch Processing, a Name scan, and placeholders…

Scan Name > Date
Change Name > Document Date followed by Name Without Date

3 Likes

How do you et “Sortable Document Date” as an option? Is that a custom metadata field?

That’s one of the Document Date submenu placeholders.

1 Like

Perhaps the extension. (.pdf) is not matched in the scan name action? @BLUEFROG might know.
Edit Yes, that’s it. If you do a scan name for the pattern (.*\.pdf) and use the action Show Warning with the text \1, you won’t see anything. That’s probably related to the setting General/Appearance/Show file name suffixes.
I think that this is a rather unfortunate decision: scan name should always scan the real name of the file, not an abbreviated one. But that’s just my opinion.

Regardless of that: DT uses the ICU flavor of regular expressions. And it uses \n to indicate the number of the capturing group in replacements. ICU REs are close enough to PCRE and JavaScript, afaik.

So I can get open this dialog, and can choose Scan Name, Date, and Change Name. But I can’t get tags to show in the field. How do I do that?
Screenshot 2024-07-25 at 7.47.15 AM

This looks like really powerful functionality that I was totally unaware of, where can I read more documentation on it? Clicking the “?” for help takes me to the page for “Sidebar: Smart Group and Rule Editor”, which seems to have a more robust version of this dialog, but I can’t find anything specifically on Batch Process, either here or in a more general search in the help. I found my way to the Appendix with the placeholders, and that shows them as surrounded by percents, but I still can’t get that to convert to a “pill” in the Change Name Field.

OK based on rkaplan’s post I could see that it was a menu, and just guessed that maybe it was available under right click, so I tried that. I can see all the placeholders now, but I do not see Sortable Document Date anywhere. Where should I see it, and how do I get it if it’s not there?

Thank you!

@chrillek thanks for all this, how do I show actions on a regex operation in devonthink?

I don’t understand the question. The actions in smart rules don’t depend on REs. But you can use RE capturing groups (\1 etc) in some of them, eg in Show warning.

I was attempting the name changes with two different tools:
Scripts > Rename > Rename using Regex
Tools > Batch Process…

I don’t see any options there for showing warnings. But I see you mentioning smart rules, so I opened that up. I’m not familiar with this tool yet, but still don’t see how to run a regex command and get warnings. I guess the more specific questions is, if you were referring to smart rules, how do you show warnings there?

Smart rules are documented in the automation chapter in DT’s user manual.
There’s no Show warning in batch operations, that’s right.

Sorry if I’m being dense but I read through those pages, and did a find on them, and searched the help files generally, and looked through the fields in the Smart Rules dialog, and I still don’t see anything about Show warnings.

I’m not at my Mac. But there’s an action called “show warning”. I can’t tell you more than that – it’s there and you can use it. Searching in the forum might help.

You need to choose the choose the desired date format. Here, I chose the one in YYYY-MM-DD format. The placeholders are described in the Appendix > Placeholders section of the built-in Help and manual

1 Like

Hi @BLUEFROG - I have a sense that Scan Text has considerable utility that many (including myself) do not fully appreciate.

Could you maybe sugest some practical use cases for this feature of Smart Rules/Batch Process?

I believe the Englist variant being referred to is Display Alert. And yes, this can be used as a light debugger for regular expressions.

Scan Text can be used in a variety of ways. For example, you could pull the number following the word Invoice in a document. For academic purposes, you could potentially look for a DOI…

image

But note it is meant for pulling one result. In my example screen capture above, if Jim and James are both in the document, only the first result would be returned. So this is not a replacement for a search function. It is for extracting a single value.

It also does not require using regular expressions. That is an option, not a requirement, and one we don’t immediately advocate.

Why is DIsplay Alert an option for Smart Rules but not for Batch Processing?

Is there any way to put that result into a new file?

Other than Display Alert (with a Smart Rule) or placing the content into a custom metadata field what can you do with it?

Batch processing isn’t made for user interaction. It’s a fire and forget mechanism. This may change in the future.

Is there any way to put that result into a new file?

There are no actions to create documents, so no. You’d have to use an Apply Script action and write your own code. However, output from actions aren’t passed to a script action at this time. This also may change in the future.

1 Like

OK for anyone searching for and finding this thread later, here’s how to get the “sortable document date” placeholder to appear in batch processing fields:

  1. Do a secondary click in the field
  2. Choose Insert Placeholder > Document Date
  3. Choose the data format option for YYYY-MM-DD (the day I’m doing this it appears as 2024-07-25)
  4. Sortable Document Date will now appear in the field.

Thanks @BLUEFROG that allowed me to rename things the way I wanted to.

1 Like