Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
Unable to figure out how to move a file to a folder after they have been matched based on an ID that they both have in their names Google apps script

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Moving Files

function movingFile() {
  const files = DriveApp.getFolderById('1jeAihkCiA--EfAl150IXrXDUa-MhYSKY');
  const folders = DriveApp.getFolderById('1xB4XTG8d1DYtthVQQqkSQCqvzv-TGWtW');
  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('_')))) {//if 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('_')));
      fldrA.push(f);
      Drive.Files.update({"parents": [{"id": f.getId()}]}, file.getId());
      fldrNames.push(file.getName().slice(0,file.getName().indexOf('_')));//added folder name to fldrNames so that it will be used again if there are more files with that name
      
    }
  }
}

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda