2

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);
})
Amit
  • 1,018
  • 1
  • 8
  • 23
imim
  • 53
  • 1
  • 8
  • I have to apologize for my poor English skill. From `How do I get the date of the last time a folder on google drive has been updated.`, you want to retrieve the last modified time of one specific folder. From `For an example, when the last file was copied into it.`, you want to check the last modified time of all files and folders under the folder. If my understanding is correct, for example, when the folder name is modified while all files and folders in the folder are not changed, what result do you want to get? And, if a file is deleted from the folder, what result do you want to get? – Tanaike May 01 '22 at 13:40
  • Do you want the same result as the `Last modified` you can see from the UI in your Google Drive? – ziganotschka May 02 '22 at 07:03
  • I would like the last modified time and date of the most recent file change within that folder. Is that clear? – imim May 03 '22 at 04:28

1 Answers1

0
  • 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.
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thanks for this. Does this mean that once I receive a listing of all folders & their id's, I would have to then make separate individual calls for each folder id & get the file modifiedTime? – imim May 04 '22 at 09:43
  • What exactly is your situation? Do you have more than one folder for which you would like to obtain the results? In this case you can use the [query operator](https://developers.google.com/drive/api/guides/search-files) `or` (e.g. `'XXX' in parents or 'YYY' in parents `). Or do you have nested folders within the parent folder? In this case you need to write a recursive function whereby you retrieve the modified time for each a file in a folder and then cheack either any of those files are folders themselves - in this case, you will need to list the children of those nested folders and so on. – ziganotschka May 04 '22 at 11:54
  • I have many folders, with each folder having many files. My current approach is making a call to the drive api to get te a list of folders, and for each folder, making a call and getting the list of files and their lastModifiedTime, and then applying the latest lastModifiedTime to the folder "lastModifiedTime". Is this the correct approach? – imim May 04 '22 at 13:31
  • Sounds tedious, but I am not aware of a better approach. However, if you do it all programmatically through looping, it shouldn't be too bad. You can have a look at [this](https://stackoverflow.com/questions/41741520/how-do-i-search-sub-folders-and-sub-sub-folders-in-google-drive/41741521#41741521) for better understanding. – ziganotschka May 04 '22 at 14:14