0

Team members upload content (regardless of file type) into a folder on Drive. I need to copy this content into another folder automatically with a trigger, and be able to move it around from there.

I cannot use a "MoveFile" function as I am not the owner of the original content.

I have already tried to copy files automatically into the destination folder, and this works, using the code below:

function CopyFiles() {
  var srcFldr = DriveApp.getFolderById("***ID***");
  var srcFiles = srcFldr.getFiles();
  var desFldr = DriveApp.getFolderById("***ID***");
  var desFiles = desFldr.getFiles();
  var dfnA = [];
  while (desFiles.hasNext()) {
    var df = desFiles.next();
    dfnA.push(df.getName());
  }
  while (srcFiles.hasNext()) {
    var sf = srcFiles.next();
    if (dfnA.indexOf(sf.getName()) == -1) {
      sf.makeCopy(sf.getName(), desFldr);
    }
  }
}

However, I need to move this copied content into other files throughout the day, yet every time I do, the same file gets copied back into the destination folder above with the new trigger, creating a permanent loop.

Is there a way of either:

  1. moving the files from the original source folder despite not being the owner of those files?
  2. copying contents only once, upon upload or modification?

Or 3) another, better, smarter way of doing this?

Thanks for your help!

Cooper
  • 59,616
  • 6
  • 23
  • 54
Gary
  • 3
  • 2
  • Just to be clear makeCopy() does not move the files it creates a whole new file in another folder. In the current state of Google Drive any given file can have only one parent. So a given file can no longer have multiple parents. If you wish to move the contents around perhaps you might like to literally copy the data. Not being familiar with the material I can not judge the practicality of such a suggestion. But moving the contents of each tab and its contained scripts is a possibility although not necessarily as simple of a script as you now have. – Cooper Jan 18 '21 at 20:05
  • Yes, you can move files when you are not the owner. Unfortunately you can't delete them by using the setTrashed method of the File class. What I usually do in those cases is to create "Processed" subfolder in the Source folder. When the copying is finished - move the file into "Processed" subfolder. When you scan for new files in the Source folder, just ignore the "Processed" subfolder. – MRB Jan 18 '21 at 20:09
  • You may be able to move files but makeCopy() does not move any files. It creates a new one. But the question remains can you copy the content and do you want to? – Cooper Jan 18 '21 at 21:46
  • The other thought is that you might have a little more freedom if you were to use a shared drive to share files. It depends upon what roles that you provide for each user but it can be set up so that everyone has the right to copy and move files into they're private drive. – Cooper Jan 18 '21 at 21:51
  • I'm not sure I understand `However, I need to move this copied content into other files throughout the day, yet every time I do, the same file gets copied back into the destination folder above with the new trigger, creating a permanent loop.`. What `content` do you mean, what kind of file is this? How are you copying it, manually? What `new trigger` are talking about? – Iamblichus Jan 19 '21 at 08:28
  • Thanks for your help! Cooper & Milan: moving the files would be the ideal solution, but I can't seem to make this work. I tried a script which kept returning an "Exception: Access denied: DriveApp" message. Iamblichus: files include .txt, .stl, .mp4, .docx, .jpg and .png. I'm trying to move these with a script, but failing that have been trying to copy them instead using the script above. The trigger is time-driven and set at every hour for now. Thanks! – Gary Jan 19 '21 at 08:54

1 Answers1

1

I'd suggest the following workflow:

  • For every file that is copied to the destination folder, store the fileId. You could use Properties Service for this.
  • When copying files from one folder to the other, check the fileId has not been stored before.

Code snippet:

function CopyFiles() {
  var srcFldr = DriveApp.getFolderById("***ID***");
  var srcFiles = srcFldr.getFiles();
  var desFldr = DriveApp.getFolderById("***ID***");
  var desFiles = desFldr.getFiles();
  var dfnA = [];
  var key = "fileIDs";
  var scriptProperties = PropertiesService.getScriptProperties();
  var property = scriptProperties.getProperty(key); // Retrieve fileIDs property
  // Get array of fileId, or empty array if no file has been copied before:
  var arrayIDs = property ? JSON.parse(property) : []; 
  while (desFiles.hasNext()) {
    var df = desFiles.next();
    dfnA.push(df.getName());
  }
  while (srcFiles.hasNext()) {
    var sf = srcFiles.next();
    // Check not only file name, but also whether fileId has been stored before:
    if (dfnA.indexOf(sf.getName()) == -1 && arrayIDs.indexOf(sf.getId()) == - 1) {
      sf.makeCopy(sf.getName(), desFldr);
      arrayIDs.push(sf.getId()); // Add fileId to array of IDs
    }
  }
  scriptProperties.setProperty(key, JSON.stringify(arrayIDs)); // Store updated array
}

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Thank you! This seems to be working fine. I'd love to know how to move files without ownership, but this is a perfect solution in the meantime. – Gary Jan 19 '21 at 15:10