Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create folders in google drive without duplicate?

the question is: How to create folders in google drive without duplicate?

I'm answering the question in this post, and I want to share this code with anyone who is searching for a similar solution or problem to solve.

like image 799
moh.sukhni Avatar asked Aug 09 '13 15:08

moh.sukhni


People also ask

How do I mass create folders in Google Drive?

In Tile Settings, select the Drive location where you want to add multiple folders/files. Make sure the + button checkbox is selected. Click OK.

Can Google Drive detect duplicates?

You probably have a few dozen duplicate files, chiefly MP3 and photos in the Google Drive. This is a handy tool that scans selected directories and finds any duplicate files inside them. The app works with all sorts of files and can also be configured to scan sub-folders within a given directory.

Can you not make a copy of a folder in Google Drive?

If you have the Google Drive app installed on your computer, you can copy a folder in Google Drive the same way you'd copy any other folder of files. Right-click on the folder, click Copy, then go wherever you want to copy the folder, right-click again, and click Paste.


1 Answers

the problem that I had, is how to create folders path in google drive without ending up with duplicates folders in the dirve!

the first function used to check where the folder is exist from its title, pass your drive instance and the folder's title and it's parents Id (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);
    }

the function used to create a folder inside the givien parents references, if the list is empty, then the folder will be created in google drive's root directory.

/**
 * 
 * @param service google drive instance
 * @param title the folder's 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 of the folders without duplicates in google drive. in order to prevent duplicate folders in google drive, the function will check whether the folder is exist or not before creating it.

/**
 * 
 * @param service google drive instance
 * @param titles list of folders titles 
 * i.e. 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;
}
like image 80
moh.sukhni Avatar answered Nov 14 '22 22:11

moh.sukhni