3

I am using Google Drive V3 API to manipulate files in a folder.

What I am currently doing to list all my files is like so:

const data = await drive.files.list({ q: `'${folderId}' in parents` });
console.log(data.files);

So basically I'm using a service account to query a folder for all its files. I'd also like to query for date. Something like createdTime < ${date}.

Reading through a few posts on this site and the documentation, there doesn't seem to be such a feature to do so. I can see that there is the option to use modifiedTime, however when I try to use it, everything just freezes up and the script run indefinitely.

Does anyone have any ideas as to how I can query this?

Edit 21/10/12 11:08

So I tried doing the following per Taneike's suggestion:

console.log('1') //this logs in the console
const data = drive.files.list({
    q: `'${folderId}' in parents and createdTime > 2012-06-04T12:00:00-08:00`,
});
//unreachable code below
console.log('2') //this doesn't log
console.log(data) //this doesn't log

So basically my script will run indefinitely, and I have to manually end the script. drive.files.list won't return anything.

ffx292
  • 542
  • 12
  • 27
  • About `query a folder for all its files`, in your situation, the folder has the subfolders and you want to retrieve all files in the subfolders? – Tanaike Oct 12 '21 at 01:09
  • So at the moment, I am able to retrieve all the files in 'Folder A' by using `{q: ''${folderID}' in parents'}`. What I would like to do is also query it by date. So for example, `{q: ''${folderId}' in parents and createdTime < ${date}'}`. – ffx292 Oct 12 '21 at 01:19
  • Could you paste the result of `console.log(q)` with `createdTime` added? – emptyhua Oct 12 '21 at 01:34
  • Thank you for replying. From your replying, I understood that you wanted to retrieve the files by `createdTime` in the specific folder, and the specific folder has no subfolders. So I proposed an answer. Could you please confirm it? If I misunderstood your question and replying, I apologize. – Tanaike Oct 12 '21 at 01:45
  • @emptyhua there are no results, the query runs indefinitely. – ffx292 Oct 12 '21 at 02:15
  • I have to apologize for my poor English skill. About `So I tried doing the following per Taneike's suggestion:`, unfortunately, it seems that your additional script doesn't correctly reflect my proposed script. Please confirm it again. Please modify to ``const data = await drive.files.list({q: `'${folderId}' in parents and createdTime > '2012-06-04T12:00:00-08:00'`});`` and `console.log(data.data)`, and test it again. – Tanaike Oct 12 '21 at 02:20

1 Answers1

3

I thought that createdTime < date can be used. And, when I saw your script, I noticed that there is a modification point. And, from your replying of So at the moment, I am able to retrieve all the files in 'Folder A' by using {q: ''${folderID}' in parents'}. What I would like to do is also query it by date. So for example, {q: ''${folderId}' in parents and createdTime < ${date}'}. –, I thought that there might be also a modification point.

When the above points are reflected in your script, it becomes as follows.

Modified script:

const data = await drive.files.list({q: `'${folderId}' in parents and createdTime < '${date}'`}).catch((err) => console.log(err.errors));
console.log(data.data);
  • In this case, please set the value of date like 2012-06-04T12:00:00-08:00 as RFC3339 format.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • According the your answer and the documentation, it should be working, however it doesn't seem to be working. I edited my main post to show what I tried. Basically the query will run indefinitely unless I manually stop the script from running. – ffx292 Oct 12 '21 at 02:13
  • @ffx292 Thank you for replying. I apologize for the inconvenience. In your added script, `'${folderId}' in parents and createdTime > 2012-06-04T12:00:00-08:00` is used. In this case, `2012-06-04T12:00:00-08:00` is not enclosed by the single quotes. In my answer, I have modified it. This is an important point. So, when you modify it to `'${folderId}' in parents and createdTime > '2012-06-04T12:00:00-08:00'`, what result will you obtain? And, in order to retrieve the file list in your script, please use `console.log(data.data)`. – Tanaike Oct 12 '21 at 02:16
  • @ffx292 By the way, I cannot understand ` the query will run indefinitely unless I manually stop the script from running.`. Can I ask you about the detail of it? – Tanaike Oct 12 '21 at 02:17
  • Oh, so I'm running this using NodeJS, so basically when do `node index.js`, it will not stop until I press CMD+C to stop it. – ffx292 Oct 12 '21 at 02:30
  • It works! I realized that I didn't put quotation marks around the `${date}`! いつもありがとうございます! – ffx292 Oct 12 '21 at 02:31