T00rk
T00rk

Reputation: 2297

Google Drive API : List files in a specific folder

I need to list all files in a drive specific folder. It's working but not really as expected. I get the whole list of files that are in this folder even if the file has been removed and placed in the trash.

How can I only get the files that are really in this folder ?

async listActiveSeries()
{
    let _RESULT = null;
    
    try
    {
        const auth = await this._makeClient();
        const googleDriveClient = google.drive({ version: 'v3', auth });

        const response = await googleDriveClient.files.list
        ({
            pageSize: 150,
            q: `'${ID_OF_THE_FOLDER}' in parents`
        });

        if(response && response.data && response.data.files)
        {
            _RESULT = response.data.files;
        }
    }
    catch(ex)
    { console.log(ex); }
    
    return _RESULT;
}

Upvotes: 1

Views: 6491

Answers (2)

theEagle
theEagle

Reputation: 29

To get the files from a specific folder you need to do this:

val files = googleSerive
        .files()
        .list()
        .setQ("mimeType='application/vnd.google-apps.folder'")
        .setQ("parents='14GyOOlAcGT_AroCwoRyXRWdalnAOykss'")
        .setSpaces("drive")
        .setFields("nextPageToken, files(id, name, mimeType)")
        .setPageSize(20)
        .execute()

Where 14GyOOlAcGT_AroCwoRyXRWdalnAOykss it the ID of your parent folder. This Id you can take it from either the Browser URL link or running a similar command as above

googleSerive.files().list()
    .setQ("mimeType = 'application/vnd.google-apps.folder'")
    .setQ("name = 'YOUR_FOLDER_NAME'")
    .setFields("nextPageToken, files(id, name)")
    .setPageSize(10)
    .execute()

Upvotes: 0

Tanaike
Tanaike

Reputation: 201603

I believe your goal as follows.

  • You want to retrieve the file list in only the specific folder.
  • You don't want to include the files in the trashbox.

In this case, how about using trashed=false to the search query? When your script is modified, it becomes as follows.

From:

q: `'${ID_OF_THE_FOLDER}' in parents`

To:

q: `'${ID_OF_THE_FOLDER}' in parents and trashed=false`
  • By adding trashed=false as AND, the files in the trash box is not retrieved.

Reference:

Upvotes: 5

Related Questions