[英]Create folder if it does not exist in the Google Drive
My Application works with Google Drive Java API.我的应用程序与 Google Drive Java API 一起使用。
I want to create folder in the root of the Google Drive only if it does not exist.我只想在 Google Drive 的根目录中创建文件夹,如果它不存在。 I am using below code to creat folder.
我正在使用下面的代码来创建文件夹。
file = service.files().insert(body).execute();
How do i check the folder existance in the root folder.如何检查根文件夹中的文件夹存在。 I have only the folder name 'Myapp', not the instance ID.
我只有文件夹名称“Myapp”,而不是实例 ID。
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.
您可以改为在搜索条件中添加标题,这样您就不必检索所有文件夹。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.