Move, Remove, and Replace information from Google Sheets with new entries using Google Script


Problem:

I have created a Google Form that logs Timestamps, a numerical value, and an image file from its respondents to a Google Sheet. The image files are saved to an "imageFolder", but when I get too many responses, my imageFolder and Google Sheet get too large. Ideally, I want my imageFolder and its Google Sheet to stay below 50 entries.

I want to move the 10 oldest images in imageFolder to waitFolder. I want to save an array of those oldest values from column "How many?" before the entry is deleted. Then I want any new entries to replace the oldest ones who's information I have already save to waitFolder and the howMany() array (myArray10).

I know I have to move 10 images from "imageFolder" to "waitFolder" using functions along the lines of:

var text1 = "Response Form";
var text2 = "imageFolder";
var text3 = "Copy of imageFolder";
var text4 = "Wait Folder"

function moveFiles(sourceFileId, targetFolderId) {
  var myFolder = DriveApp;
  var files = myFolder.getFileById(sourceFileId).getFiles()
  while (files.hasNext()) {
    var file = files.next());
    var dest = myFolder.getFolderById(targetFolderId);
    dest.addFile(file);
    var pull = DriveApp.getFolderById(sourceFolderId);
    pull.removeFile(file);
  }
}


function getMyId(text) {                   
  var Ids = [];
  var myFolder = DriveApp;
  var folderIter = myFolder.getFoldersByName(text);
  var folder = folderIter.next();
  var folderIter = folder.getFiles();
  while(folderIter.hasNext()) {
    var file = folderIter.next();
    var fileId = file.getId();
    Ids.push(fileId);
  }
  return Ids
}

function getMyId10(text) {                   
  var Ids = [];
  var myFolder = DriveApp;
  var folderIter = myFolder.getFoldersByName(text);
  var folder = folderIter.next();
  var folderIter = folder.getFiles();
  for (var i = 0; i < 10; i++) {  //Take the first 10
    while(folderIter.hasNext()) {
      var file = folderIter.next();
      var fileId = file.getId();
      Ids.push(fileId);
    }
  }   
  return Ids
}

function main() {
  var imageFolderId = getMyId(text2);
  var imageFolderId10 = getMyId10(text2);
  var waitFolderId = getMyId(text4);
  var Copy_imageFolderId = getMyId(text3);
  moveFiles(imageFolderId, Copy_imageFolderId); //make a copy of imageFolder
  moveFiles(imageFolderId10, waitFolderId);     //Move first 10, remove from original
}

How can I move images from imageFolder to Copy_imageFolder? How can I move the 10 oldest images from imageFolder to waitFolder? How can I remove the 10 oldest images from imageFolder? How can I limit the number of rows in my spreadsheet using Google script? How can I overwrite my oldest rows with new entries/rows?

edit1: I am getting unexpected tokens in every function, and I am unsure why? It seems to pop up in the while loop of my function getMyId().

edit2: I see now why I was getting unexpected tokens. It seems that I was being irresponsible with my loops. I have replaced my 'while's with 'for's to amend this mistake.

edit3: I removed some unnecessary snippets of code to make it easier to follow.

edit4: enter image description here Here is what my Form Response looks like in my Spreadsheet. The images are saved to subfolder imageFolder. But I can't grab the 10 array elements I want from the spreadsheet using howMany(). I also can't seem to move any of the files anywhere. When I call on moveFiles(), I get an unexpected error as soon as it asks for my DriveApp. How do I make my moveFiles() move my images from source to target folders?


Solution:

Perhaps this is what you were looking for:

function moveFiles(sourceFolderId, targetFolderId) {
  var srcFolder=DriveApp.getFolderById(sourceFolderId);
  var desFolder=DriveApp.getFolderById(targetFolderId);
  var files=srcFolder.getFiles();
  while(files.hasNext()) {
    var file=files.next();
    desFolder.addFile(file);
    srcFolder.removeFile(file)
  }
}

Recent Tips

  1. How can I get rid of jerkiness in WinForms scrolling animation?
  2. Update MySQL table column from another table entities
  3. Shoot fireball once in unity C#
  4. Adding a table of contents to a Microsoft Word Document using vbs
  5. Change locale in android app (onto Hindi)
  6. How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
  7. Sharepoint with silverlight app
  8. What's the best way to write robots.txt for github pages using multiple repos?
  9. Biopython: Cant use .count() for biopython
  10. How can I find out the token balance of an address?
  11. ref value is undefined in vue (modal, textarea, $refs)
  12. Azure - HDInsight Hbase Data Insertion Failed
  13. SignalR overwriting OnConnected(), OnDisconnected()
  14. DatePickerDialog displays with two borders
  15. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  16. hiding the autocomplete list when user click outside the textbox is not working as expected
  17. JSF IceFaces basic problem with redisplaying input value
  18. How to validate material ui TextField in reactjs?
  19. Go and MongoDB connection won't work with panic log "no reachable server"
  20. WordPress Posts Pagination Not Working
  21. F# sprintf won't print in interactive console
  22. Spring Integration get FTP files recursively with outbound-gateway
  23. Jade mixins not getting working from external file
  24. Can not access defined exports from the webpack bundle?
  25. Completely new to Node.js - API Programming
  26. Formatting Compare-Object Ouput
  27. Add dynamically added textbox value from User Control to main form
  28. Create a ByteBuf in Netty 4.0
  29. Is it possible to do computation before super() in the constructor?
  30. Q-learning Updating Frequency