(NodeJS) Google Drive API - Folder listing modifiedTime

Viewed 110

How do I get the date of the last time a folder on google drive has been updated.

For an example, when the last file was copied into it.

Currently I'm using the following code to get back the date when the folder was created:

const drive = google.drive({ version: 'v3', auth});
console.log("Calling listing")

await drive.files.list({
  pageSize: 1000,
  fields: 'nextPageToken, files(id, name, modifiedTime)',
  q: "mimeType='application/vnd.google-apps.folder'",
}, (err, dataResponse) => {
    if(err) {
     res.json("Error " + err);   
    }
    const folders = dataResponse.data.files;
    console.log(folders);
    res.json(folders);
})
1 Answers
  • In agreement with the UI behavior, the Last Modified time the API will give you for the folder itself will be different from the Last Modified time of the latest modified file within this folder
  • This is due to Google's Drive hierarchy where a change of the Last Modified time of a file does not necessary change the Last Modified time of the parent folder
  • In any case, if you would like to know the Last Modified time of the folder itself, you need to perform a Files: get request specifying the id of the folder as fileId and setting fields to modifiedTime.

Sample:

...
await drive.files.get({
  fileId: "XXX"
  fields: "modifiedTime"
},
...
  • If you would like to access the modifiedTime property of the files in the folder, you can do it with Files: list , setting q to 'XXX' in parents, whereby XXX is the id of the parent folder, and setting fields to files/modifiedTime.

Sample:

...
await drive.files.list({
  fields: "files(modifiedTime)",
  q: "'XXX' in parents"
}
...

This will return you a response like

{
 "files": [
  {
   "modifiedTime": "2021-10-09T14:22:58.306Z"
  },
  {
   "modifiedTime": "2021-10-07T17:38:56.515Z"
  },
  {
   "modifiedTime": "2021-09-28T16:28:12.476Z"
  },
  {
   "modifiedTime": "2021-09-27T16:13:58.201Z"
  }
 ]
}
  • You can then programmatically determine the newest timestamp from the result list.
Related