Home > Mobile >  Unable to figure out how to move a file to a folder after they have been matched based on an ID that
Unable to figure out how to move a file to a folder after they have been matched based on an ID that

Time:03-15

In Google Drive I am trying to compare an id in a file name against folders named with that id, and if they match, to move that file to that folder.

Some of the problems that the program has to account for are:

  1. Since the id is generated from the customer's name it does not have a set length
  2. The id could be anywhere in the file name, not just at the beginning or end
  3. There can be multiple files with different names containing the same id, so the program has to loop

Things that make this easier:

  1. All of the folders are in the same parent folder and have the id as their name, so the program can use the list of folder names as a list of the ids
  2. The files are all in the same parent folder, and there will be no other files in the folder, so the loop can just run until the folder is empty

So far I have written a code that successfully:

  1. Accesses both parent folders by id
  2. Loops through the file folder until it has checked all files, and pushes the file names to an array called "fileNames"
  3. Loops through the folders and compares the current folder's name to the file names for a match, and pushes the folder names to an array called "childNames"

I have created two files in the "files" folder to test with, "john1111_test.pdf" and "thomas2222_test.pdf", and two folders in the "childName" folder "john1111" and "thomas2222"

The part that I am stuck on is copying the file to the folder it matched with.

var childFolder = DriveApp.getFolderById('****').getFolders(); // Folder containing folders
var files = DriveApp.getFolderById('****').getFiles(); // Folder containing the files

  var fileNames = [];
   while (files.hasNext()) {
     var file1 = files.next();
     var fName = file1.getName()

     Logger.log(fName);

     fileNames.push(fName);
     
   }

  var childNames = [];
   while (childFolder.hasNext()) {
     var child = childFolder.next();
     var cdName = child.getName();

     Logger.log(cdName);
     
     childNames.push(cdName);
     
     const match = fileNames.find(element => {
       if (element.includes(cdName)) {

// This is the area that I am having issues with         
         let folderMatch = childFolder.getFoldersByName(cdName);
         let fileMatch = files.getFilesByName(element);
         folderMatch.addFile(fileMatch)


         return true;
        }
     });
     console.log(match);
   }

I receive an error saying that foldermatch.addFile is not a function. I have tried cdName.addFile(element) as well.

Any help would be appreciated

CodePudding user response:

Moving Files

function movingFile() {
  const files = DriveApp.getFolderById('files folder id');
  const folders = DriveApp.getFolderById('folders folder id');
  const fldrs = folders.getFolders();
  let fldrNames = [];
  let fldrA = [];
  while (fldrs.hasNext()) {
    let fldr = fldrs.next()
    fldrNames.push(fldr.getName());
    fldrA.push(fldr);
  }
  const fs = files.getFiles();
  while (fs.hasNext()) {
    let file = fs.next();
    let idx = fldrNames.indexOf(file.getName().slice(0,file.getName().indexOf('_')));
    if(~fldrNames.indexOf(file.getName().slice(0,file.getName().indexOf('_')))) {//iif folder  is present move it here
     //SpreadsheetApp.getUi().alert(file.getName().slice(0,file.getName().indexOf('_')));
      Drive.Files.update({"parents": [{"id": fldrA[idx].getId()}]}, file.getId());
    } else {//if it is not then create it first and xfer
      let f = folders.createFolder(file.getName().slice(0,file.getName().indexOf('_')));
      Drive.Files.update({"parents": [{"id": f.getId()}]}, file.getId());
    }
  }
}

Before:

enter image description here

enter image description here

After:

enter image description here

enter image description here

Need to enable Drive API Version 2

  • Related