Unable to extract date from file name

Hello There,
I’me trying for 3 hours to extract date from file name.
I’ve read 10 thread about this.
I’ve tried using chatgpt.
Unable to understand if my problem comes from localisation or whaever.
Unable to understand if there is many different date for the same file.

My goal is to convert part of file name in “creation date” (ideally) or at least in “date of personnal meta data”.

When asking many many script to ChatGPT unable to correct it :

tell application "DEVONthink 3"
    set theRecords to (every record of current database whose type is "file")
    
    repeat with theRecord in theRecords
        set theName to name of theRecord
        set theDate to extract date from theName
        if theDate is not missing value then
            set creation date of theRecord to theDate
        end if
    end repeat
end tell

on extract date from theName
    set dateString to ""
    set dateRegex to "(?<=-\\s)\\d{8}(?=\\s-)"
    try
        set dateString to do shell script "echo " & quoted form of theName & " | grep -Eo " & quoted form of dateRegex
    end try
    if dateString is not "" then
        set yearPart to text 1 thru 4 of dateString
        set monthPart to text 5 thru 6 of dateString
        set dayPart to text 7 thru 8 of dateString
        set theDate to date (yearPart & "-" & monthPart & "-" & dayPart)
        return theDate
    else
        return missing value
    end if
end extract date

About date I have some other questions :

What are the differences between création date from finder, creation date from DT point of view?

Any help much appreciated. I have the last 3.000+ files date to extract. :slight_smile:

DEVONthink gets the creation date from the Finder when the document is added to the database.
And a PDF metadata date is not the file’s creation date. You are talking about two different things there. See the Appendix > Hidden Preferences section of the built-in Help and manual for the UsePDFDocumentDates command. Also, it is not required to use Terminal to enable this function. Read the note at the top of the section. Lastly, you will need to do a File > Rebuild Database to modify the creation dates on the already imported documents.


When asking many many script to ChatGPT unable to correct it :slight_smile:

This is unsurprising and not an avenue I would even bother with. At the best, you’ll get a poorly written and slightly functional bit of code but (1) the code likely won’t be fully effective and (2) what will you have learned?


Your filenames in the examples here are very uniform. Is this what you expect from all the input documents?

3 Likes

Given that I prefer Human Intelligence 1.0 vastly over ChatGPT in whatever version, I’m not going to fix this mess of code.
Just some notes:

  • It would certainly help if you stated your goals clearly in written, giving example file names and the desired results. On one hand, writing forces the writer to clear their thoughts and express their desires clearly. On the other hand, it makes it easier for the readers to grasp what the person on the other end intends.
  • As it stands, this script will never run in a smart rule. Please read the chapter “Automation” in the DT manual to learn how these scripts must look.
  • The regular expression is way too complicated for my taste. If you know that all dates are from this century, something like (2\d{7}) should do.
  • Instead of using AppleScript and some half-baked shell commands, one could use JavaScript, which already supports regular expressions.
  • In that case, I’d use a regular expression like -\s+(2\d{3})(\d\d)(\d\d)\s+- and access year, month and day by the expression’s capturing groups 1, 2 and 3.

And I, of course, concur with @bluefrog’s assessment: ChatGPT will at best throw bad, working code at you. At worst, the code doesn’t work. And you never learn anything from it.

2 Likes

Assuming the names are indeed that uniform, here’s an example using a custom metadata attribute with a Single-Line Text data type…

And the resulting metadata applied…

2 Likes

First of all, TY for reply.

In fact I can manage large building site and engine management tuning but really really I don’t know to code. I’ve tried CHATGPT just because I thought it was easier. I do not like to use CHATGPT not even to summarize a 100 word pdf :wink:

Yes they are uniform this way!

Thank you for reply.
I have to admint IA was not helpful and does not help nor to learn or understand. I do really have no time to learn to code, not this year at least :confused:
For example I absolutely don’t know what are the differnces between apple and java script.

What I can they is I have 2000 and even 3000 files encoded this way :

“Name of the company - YYYYMMDD - Objet of the file”.

In fact I would like this date the be fullfill this way
Not to create a new custom data but convert in real date.

I’ve already suceed to create this … But unable to convert nor this or directly part of the file name into a real date

Actually I’ve only found this but can’t change date using filename

Maybe it is just not possible to transform 8 digit in date. Sad about this. None of previous thread found the answer.

It is not possible to change every type of custom metadata via a smart rule without using the Execute Script function.
Is there a reason it must be a Date data type?

@BLUEFROG did show you how to get a date in a custom metadata field. You cannot, however, set the creation or modification date to an arbitrary value, it seems. At least not with a simple smart rule. A script, however, can do that

function performsmartrule(records) {
  const app = Application("DEVONthink 3");
  records.forEach(r => {
     r.creationDate = app.getCustomMetaData({for: "DateDuFichier", from: r}));
  })
)

Here, DateDuFicher is a custom metadata field of type date, which you set using following @BLUEFROG’s example. Add this script as an additional “execute script” action after Change DateDuFichier, selecting “JavaScript” and adding the code to the “Edit Script” box (delete everything that is in there first!).
You end up with the file date as the creation date, and an unnecessary custom metadata field “DateDuFichier”. Tant pis.

1 Like

Hello.
It is absolutely not mandatory. It is a must have the way I work but can try with an another way.

Thank you for this script.
I’m going to give a try.
Previouly I’ve create a custom metadata because I thought it was the way to proceed. Not a big deal if I do not use anymore. I prefer original embedded (?) data instead of custom metadata, for sure
But finally I can try to modify “create date” or “modify date”.
Thank you again.

As the script stands now, it relies on the custom metadata field “DateDuFichier”. That is by no means necessary, the script could simply extract the date information from the file name and set the creation date accordingly.

Not complicated, just more code:

function performsmartrule(records) {
  const app = Application("DEVONthink 3");
  records.forEach(r => {
     const match = r.name().match(/(.*?)\s+-\s+(\d{4})(\d\d)(\d\d)\s+-\s+(.*)/);
     if (match) {
        const company = match[1];
        const objet = match[5];
        const date = new Date(`match[2]-match[3]-match4`);
        r.creationDate = date;
        app.addCustomMetaData(company, {for: 'company', to: r});
        app.addCustomMetaData(objet, {for: 'objet', to: r});
     }
  })
)

This code is not tested at all. It does not set the modification date (that would be an additional line), since I don’t see the point of having identical creation and modification dates. If needed, that’s left as an exercise to the reader.

This script can be the sole action in a smart rule (Execute Script/JavaScript). No need for “Scan Name” etc.

2 Likes