How to create folders on a Google drive without duplicates?

Q: How do I create folders in Google Drive without duplicates?

I answer this question in this post, and I want to share this code with anyone looking for a solution or problem to solve.

+7
java android google-drive-sdk
source share
1 answer

The problem I am facing is how to create a folder path in Google Drive without ending up with duplicate copies in the dirve directory!

The first function used to check where the folder exists from its name transfers the instance of your disk and the title of the folder, and the parent identifier (not the title):

/** * * @param service google drive instance * @param title the title (name) of the folder (the one you search for) * @param parentId the parent Id of this folder (use root) if the folder is in the main directory of google drive * @return google drive file object * @throws IOException */ private File getExistsFolder(Drive service,String title,String parentId) throws IOException { Drive.Files.List request; request = service.files().list(); String query = "mimeType='application/vnd.google-apps.folder' AND trashed=false AND title='" + title + "' AND '" + parentId + "' in parents"; Logger.info(TAG + ": isFolderExists(): Query= " + query); request = request.setQ(query); FileList files = request.execute(); Logger.info(TAG + ": isFolderExists(): List Size =" + files.getItems().size()); if (files.getItems().size() == 0) //if the size is zero, then the folder doesn't exist return null; else //since google drive allows to have multiple folders with the same title (name) //we select the first file in the list to return return files.getItems().get(0); } 

function used to create a folder inside parental parent links; if the list is empty, the folder will be created in the root directory of the Google root directory.

 /** * * @param service google drive instance * @param title the folder title * @param listParentReference the list of parents references where you want the folder to be created, * if you have more than one parent references, then a folder will be created in each one of them * @return google drive file object * @throws IOException */ private File createFolder(Drive service,String title,List<ParentReference> listParentReference) throws IOException { File body = new File(); body.setTitle(title); body.setParents(listParentReference); body.setMimeType("application/vnd.google-apps.folder"); File file = service.files().insert(body).execute(); return file; } 

The third function used to create a directory path to folders without duplicates on a Google drive. to prevent duplicate folders on the Google drive, the function will check if the folder exists or not before it is created.

 /** * * @param service google drive instance * @param titles list of folders titles * ie if your path like this folder1/folder2/folder3 then pass them in this order createFoldersPath(service, folder1, folder2, folder3) * @return parent reference of the last added folder in case you want to use it to create a file inside this folder. * @throws IOException */ private List<ParentReference> createFoldersPath(Drive service,String...titles) throws IOException { List<ParentReference> listParentReference = new ArrayList<ParentReference>(); File file = null; for(int i=0;i<titles.length;i++) { file = getExistsFolder(service, titles[i], (file==null)?"root":file.getId()); if (file == null) { file = createFolder(service, titles[i], listParentReference); } listParentReference.clear(); listParentReference.add(new ParentReference().setId(file.getId())); } return listParentReference; } 
+4
source share

All Articles