I could find only ways to get the root folder id from google drive api -[REFERENCE]: Google Drive API v3 getting root folder Id in java . I have to get the folder id by giving a name. Is there anyway i could get Folder id of a directory in Drive by passing its name.
Asked
Active
Viewed 1.4k times
3 Answers
2
You can search for a specific folder with these parameters:
name = 'your-folder-name'
mimeType = 'application/vnd.google-apps.folder'
The mime type makes sure, that you are only searching for folders. For more information, take a look at the documentation of the search parameters here.

Tobias Geiselmann
- 2,139
- 2
- 23
- 36
-
@GaneshR Can you give me the example how to execute this code? – XamDev Mar 08 '19 at 11:55
1
Use the following to find the folder id with folder name. if such folder is not found it will result folderId as 0.
let folderName = "myFolder";
let result = await drive.files.list({
q: "mimeType='application/vnd.google-apps.folder' and trashed=false",
fields: 'nextPageToken, files(id, name)',
spaces: 'drive',
}).catch(e => console.log("eeee", e));
let folder = result.data.files.filter(x => x.name === folderName);
var folderId = folder.length?folder[0].id:0;
console.log(folder.id)

Selva Mary
- 658
- 1
- 4
- 17
-
You are a life saver @Selver Mary. Exactly what I was looking. I will give 1000 upvotes if I could. Thanks. – Akintomiwa Opemipo Jun 09 '21 at 12:31
0
// $client will be get auth detail
$server = new Google_Service_Drive ( $client );
$metaDataArray= array(
q=>"mimeType='application/vnd.google-apps.folder' and name='folder_name' and trashed=false",
spaces =>'drive',
fields =>'nextPageToken, files(id, name)',
);
$fl = $server->files->listFiles( $metaDataArray);
This is in php . you can just convert to java

fahad sulaiman
- 73
- 6