Home > OS >  google drive moving files into folders - iterator has reached the end
google drive moving files into folders - iterator has reached the end

Time:02-18

I wrote a little code, it should grab all files in a folder and one by one put into a folder based on the first 10 character of the filename. If no folder with the name, creates one. Its working, but randomly stops with an error. (see pic below)

What really strange is, if i restarting it, it continues and after a couple of restarts it can finish the job. Why is it randomly stopping? Could you help me with this please? Its driving me crazy.

Thank you in advance.

function sort() {
  
  var inputFolder = DriveApp.getFolderById("thefolderid12343434343");

  var files = inputFolder.getFiles();
  let folders = inputFolder.getFolders();
  let folderNames = [];

  while (folders.hasNext()) {
    var folder = folders.next();
    folderNames.push(folder.getName());
  }

  while (files.hasNext()) {

    var file = files.next();
    var fileName = file.getName();
    var destinationFolderName = fileName.substring(0,10); //for x characters

    if (folderNames.includes(destinationFolderName)) {
    
      var destinationFolderId =inputFolder.getFoldersByName(destinationFolderName).next().getId();
      var processedFolder = DriveApp.getFolderById(destinationFolderId);
      file.moveTo(processedFolder);

    } else {

      let newFolder = inputFolder.createFolder(destinationFolderName);
      let destinationFolderId = newFolder.getId();
            try{
        var processedFolder = DriveApp.getFolderById(destinationFolderId);
      file.moveTo(processedFolder);
      }
      catch(error){
        Logger.log(destinationFolderName);
      }
      folderNames.push(destinationFolderName);
      
    }
    
  
  }

}


error code

runtimes

CodePudding user response:

Nor sure if it's the cause of the error but I suspect you can simplify the code a bit.

Instead of this:

var destinationFolderId =inputFolder.getFoldersByName(destinationFolderName).next().getId();
      var processedFolder = DriveApp.getFolderById(destinationFolderId);
      file.moveTo(processedFolder);

You can use this:

var processedFolder = inputFolder.getFoldersByName(destinationFolderName).next();
file.moveTo(processedFolder);

Perhaps it helps if you won't call DriveApp here. And the same thing in the else branch.

I haven't tried the code, I can be wrong.

CodePudding user response:

I noticed that this line of code does not have hasNext() to check if the folder/s exist.

if (folderNames.includes(destinationFolderName)) {
  var destinationFolderId =inputFolder.getFoldersByName(destinationFolderName).next().getId();
  var processedFolder = DriveApp.getFolderById(destinationFolderId);
  file.moveTo(processedFolder);
}

Usually, Exception: Cannot retrieve the next object: iterator has reached the end. occurs when the Iterator can't find the item in the folder or the folder is empty.

Instead, try enclosing it with an if-statement with hasNext():

if(inputFolder.getFoldersByName(destinationFolderName).hasNext()){
   var destinationFolderId = inputFolder.getFoldersByName(destinationFolderName).next().getId();
   var processedFolder = DriveApp.getFolderById(destinationFolderId);
   file.moveTo();
}

Note: Using inputFolder.getFoldersByName(destinationFolderName).next().getId(); directly without iteration will only get the id of the first folder that matches the destinationFolderName name.

Reference:

  • Related