Create folder if it does not exist in Google Drive

My application works with the Google Drive Java Java API.

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

file = service.files().insert(body).execute(); 

How to check the existence of a folder in the root folder. I have only the folder name "Myapp" and not the instance ID.

+6
source share
1 answer
 Files.List request = service.files().list().setQ( "mimeType='application/vnd.google-apps.folder' and trashed=false"); FileList files = request.execute(); 

Now you can browse all the folders in the β€œfiles” and check if any of the folders has the desired header.

Remember to scroll through all pages with:

 request.setPageToken(files.getNextPageToken()); 

Edit:

Perhaps you could take a look at this site . You can add a title to your search criteria, instead you do not need to extract all the folders.

+9
source

Source: https://habr.com/ru/post/927315/


All Articles