'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:
- Since the id is generated from the customer's name it does not have a set length
- The id could be anywhere in the file name, not just at the beginning or end
- There can be multiple files with different names containing the same id, so the program has to loop
Things that make this easier:
- 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
- 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:
- Accesses both parent folders by id
- Loops through the file folder until it has checked all files, and pushes the file names to an array called "fileNames"
- 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.
Solution 1:[1]
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:
After:
Need to enable Drive API Version 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |