Create folder if it does not exist in the Google Drive

后端 未结 2 1765
生来不讨喜
生来不讨喜 2021-01-05 05:31

My Application works with Google Drive Java API.

I want to create folder in the root of the Google Drive only if it does not exist. I am using below code to creat fo

相关标签:
2条回答
  • 2021-01-05 05:42
    Files.List request = service.files().list().setQ(
           "mimeType='application/vnd.google-apps.folder' and trashed=false");
    FileList files = request.execute();
    

    You could now go through all folders in "files" and check if any of the folders have the searched title.

    Don't forget to loop through all pages with:

    request.setPageToken(files.getNextPageToken());
    

    Edit:

    Maybe you could have a look at this site. You can add the title in your search criteria instead so you do not have to retrieve all the folders.

    0 讨论(0)
  • 2021-01-05 05:48
    Files.List request=service().files().list().setQ(
                       "mimeType='application/vnd.google-apps.folder' and trashed=false and name='"+folderName+"'");
            FileList files = request.execute();
    

    Above line of code is used to get the list of all the files/folder with given name. If files are empty, it means files or the folder with given name doesn't exist and you can create a new folder with below mentioned line of code:

    File fileMetadata = new File();
            fileMetadata.setName(folderName);
            fileMetadata.setMimeType("application/vnd.google-apps.folder");
    
            File file = service().files().create(fileMetadata)
                .setFields("id")
                .execute();
    
    0 讨论(0)
提交回复
热议问题