Jason
Jason

Reputation: 41

Merge folder with the same name in google drive using google script

Who knows how to merge folder with the same name in google drive using google script?

Step 01: Find if any duplicate folder in the root-Folder;

Step 02: If yes, choose one folder as main-folder, and then move all files in the other folder(with the same name) to this main-folder;

Step 03: Delete the other folder(with the same name).

Many thanks in advance.

Merge Folder with the same name under the same Folder

Upvotes: 4

Views: 16403

Answers (2)

Břeťa
Břeťa

Reputation: 122

My approach:

  1. Take parent folder in which you want to check for folders with same name
  2. Loop over all subfolders and get their names
  3. Find all folders within parent folder with each found folder name
  4. Specify first iteration for given folderName as destination folder
  5. If there is more than one folder with given name, move all files and folders from this folder to destination folder and trash that folder

Try this:

I have succesfully tested this function on some junk data.

function mergeDuplicateFolders() {

  var parentFolder = DriveApp.getFolderById('{paste your folder ID here}'); //parent folder which will be scanned for duplicates
  var childFolderIterator = parentFolder.getFolders(); //all folders within parent folder

  //iteration over all folders in parent directory
  while (childFolderIterator.hasNext()) {
    var childFolder = childFolderIterator.next();
    var childFolderName = childFolder.getName();
    var childSameNameIterator = parentFolder.getFoldersByName(childFolderName);

    //just check if folder with givn name exist (there should be at least one)
    if(childSameNameIterator.hasNext()) {
      var destFolder = childSameNameIterator.next();

      //iteration over 2nd and all other folders with given name
      while (childSameNameIterator.hasNext()) {      
        var toMergeFolder = childSameNameIterator.next();
        var filesToMove = toMergeFolder.getFiles();
        var foldersToMove =  toMergeFolder.getFolders()

        //iteration over all files    
        while (filesToMove.hasNext()) {
          var file  = filesToMove.next();
          moveFile(file, destFolder);
        }

        //iteration over all subfolders    
        while (foldersToMove.hasNext()) {
          var folder  = foldersToMove.next();
          moveFolder(folder, destFolder);
        }
       //trashes empty folder
        toMergeFolder.setTrashed(true);
      }
    }

  }

}

//custom function which removes all parents from speciefied file and adds file to new folder
function moveFile(file, destFolder) {
  var currentParents = file.getParents();
  while (currentParents.hasNext()) { // be careful, this loop will remove all parents from the file... if you want to have this file in multiple folders you should add if statement to remove it only from specified folder
    var currentParent = currentParents.next();
    currentParent.removeFile(file); 
  }
  destFolder.addFile(file);
  Logger.log("File moved to " + destFolder.getName());
}

//custom function which removes all parents from speciefied folder and adds that folder to new one
function moveFolder(folder, destFolder) {
  var currentParents = folder.getParents();
  while (currentParents.hasNext()) { // be careful, this loop will remove all parents from the folder... if you want to have this folder in multiple folders you should add if statement to remove it only from specified folder
    var currentParent = currentParents.next();
    currentParent.removeFolder(folder);
  }
  destFolder.addFolder(folder);
  Logger.log("Folder moved to " + destFolder.getName());
}

Your application:

You can of course edit first two lines of this function and add parentFolderId as parameter of that function. Then you can use this function in your own iteration and check multiple folders for duplicates.

function mergeDuplicateFolders(parentFolderId) {

  var parentFolder = DriveApp.getFolderById(parentFolderId);

  //...continue with the code from above

Warnings

Be careful. In Google Drive you can have one file in multiple folders. This code will remove files (in folders with same name) from all parent folders and moves them only in one. If you prefere removing it only from the folder with same name, you need to add some if statement in moveFolder() and moveFile() functions.

Google script allows to run custom function only for (approx.) 6 minutes. If you have large amout of folders to check, you should make some workaround. You can add e.g. execution time limit. Example is here: Prevent Google Scripts from Exceeding the Maximum Execution Time Limit

EDIT:

Of course there is much simpler way to remove file only frome merged folder... Remove both custom functions and edit both iterations this way... I didn't realize it first.

//iteration over all files
while (filesToMove.hasNext()) {
  var file = filesToMove.next();
  toMergeFolder.removeFile(file); 
  destFolder.addFile(file);
}

//iteration over all subfolders
while (foldersToMove.hasNext()) {
  var folder = foldersToMove.next(); 
  toMergeFolder.removeFolder(folder);
  destFolder.addFolder(folder);
}

Upvotes: 2

Teyam
Teyam

Reputation: 8102

Class Folder, currently, doesn't have existing method to move files from one folder in Google Drive to another.

And as far as I know, there is an existing issue on Seeing duplicate files after sync on desktop Drive version 1.14. However, you may follow the given workaround in the forum as follows:

Step 01: How to Find duplicates files (by name and/or size) in a specific folder of my google drive

Step 02: How do I copy & move a file to a folder in Google Apps Script?

Step 03: You can use removeFolder(child) method from Class Folder, which can only be used to remove a given folder from a current folder.

I hope that helps.

Upvotes: 0

Related Questions