Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How does a script determine if the last modified date of a file is today?

Guide ,
Jun 25, 2025 Jun 25, 2025

For example, I want to determine the last modified date of my.json.
If it is not today, I perform operation A, if it is, I skip operation A. Execute

 

Thank you very much.

TOPICS
Scripting
148
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 26, 2025 Jun 26, 2025

Here's one way:

(function () {

    var file = File.openDialog();

    if (!file)
        return;

    var modified = file.modified,
        today = new Date();

    if (
        modified.getFullYear() === today.getFullYear()
        && modified.getMonth() === today.getMonth()
        && modified.getDate() === today.getDate()
    )
        alert('The file "' + decodeURI(file.name) + '" was modified today.');

    else
        alert('The file "' + decodeURI(file.name) + '" was modified before today.');

})();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 26, 2025 Jun 26, 2025

Hi m1b.

Thank you.

I don't open the file, I still want to check if the last modified date of my.json is today.

The myJSONpath below doesn't seem right.

 

myJSONpath=decodeURI(myJSONpath );

Get this:

66886.png

function getJson() {
    myJSONObject = evalJSON(File($.fileName).parent.parent + '/PubLib/my.json');
    if (!myJSONObject)
        alert('Failed to load JSON.');
    myJSONpath = (File($.fileName).parent.parent + '/PubLib/my.json');
    myJSONpath=decodeURI(myJSONpath );

}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 26, 2025 Jun 26, 2025

I've often tried to mess with it, but for some reason it suggests that there's an error in line 49

dublove_0-1750955366598.jpeg

 

//@include '../PubLib/pubLib.jsx';
//@include '../PubLib/json.jsx';
function getJson() {
    myJSONObject = evalJSON(File($.fileName).parent.parent + '/PubLib/my.json');
    if (!myJSONObject)
        alert('Failed to load JSON.');
    var str = decodeURI((File($.fileName).parent.parent + '/PubLib/my.json'));
    //alert(myJSONObject);
    path1 = str.substring(1, 2);
    //alert(path1);
    path2 = str.substring(2, str.length - 0);
    myJSONpath = path1 + ':' + path2;
    //alert(myJSONpath);
}

function readFile(path) {
    var file = File(path);
    if (!file.exists)
        return;
    file.open('r');
    var content = file.read();
    file.close();
    return content;
};

function evalJSON(path) {
    json = readFile(path);
    if (!json)
        return;
    return JSON.eval(json);
};


getJson();
lastMoDate();
function lastMoDate() {

    var file = myJSONpath;

    //alert(myJSONpath);

    if (!file)
        return;

    var modified = file.modified,
        today = new Date();
    alert(file);

    if (
        modified.getFullYear() === today.getFullYear()
        && modified.getMonth() === today.getMonth()
        && modified.getDate() === today.getDate()
    ) {
        alert('The file "' + decodeURI(file.name) + '" was modified today.');
    }
    else {
        alert('The file "' + decodeURI(file.name) + '" was modified before today.');
    }
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 26, 2025 Jun 26, 2025
LATEST

I modified the code above:
myJSONpath = path1 + ':' + path2;
It should not contain file:/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines