Reputation: 3
I want to code a function who copy my files of one specific folder into another folder who have some specifics folders creates with another function with the descriptions of my files.
So into my while loop filesIter.hasNext()
to check the description of all my files I need to implement an other while folderIter.hasNext()
to check the description of my folders. And if the two descriptions are equal, I copy the file into the good folder.
But for the moment I can do it just into my first Folder but I don't know how I can check the next folder while the description of my file not equal to the description of my folder.
function CopySheetsIntoGoodFolder2() {
var folderFiles = DriveApp.getFolderById('123C');
var filesIter = folderFiles.getFiles();
while (filesIter.hasNext()) {
var files = filesIter.next();
var descriFiles = files.getDescription();
var folder = DriveApp.getFolderById('123D');
var folderIter = folder.getFolders();
var folders = folderIter.next();
var descriFolder = folders.getDescription();
while (descriFiles != descriFolder) {
folderIter.hasNext().next();
}
if (descriFiles == descriFolder) {
files.makeCopy(folders);
}
}
}
I know that is my second while who is wrong but I don't know how to do what I want. I've tried so much things but now i'm blocked...
Upvotes: 0
Views: 51
Reputation: 4247
You need an outer loop to get the file descriptions. And an inner loop (inside the outer loop) to get the folder descriptions and match them to the file descriptions.
Try this:
function CopySheetsIntoGoodFolder2() {
var folderFiles = DriveApp.getFolderById('123C');
var filesIter = folderFiles.getFiles();
// This can be outside the loop. No point getting the same folder each time
var folder = DriveApp.getFolderById('123D');
// Declare variable
var files, folders, descriFiles, descriFolder, folderIter;
// Loop through files [Outer loop]
while (filesIter.hasNext()) {
files = filesIter.next(); // The file
descriFiles = files.getDescription(); // File description
folderIter = folder.getFolders(); // Get sub-folders
while (folderIter.hasNext()) { // Loop through sub-folders [Inner loop]
folders = folderIter.next(); // Get a folder
descriFolder = folders.getDescription(); // Get its description
if (descriFiles == descriFolder) { // Check is descriptions match
files.makeCopy(folders);
break; // Break loop if the descriptions match and the file has been copied.
}
}
}
}
Upvotes: 1