Search code examples
google-apps-scriptfile-copying

When copying files using Apps Script from one folder to another any "Apps Script" files being copied end up in MyDrive not the specified folder - why?


I use this Apps Script to make backup copies of some files:

var sourceFolderId = "xxxxxx";
var sourceFolder = DriveApp.getFolderById(sourceFolderId);
var latestFiles = sourceFolder.getFiles();
var backupFolderId = "yyyyyy";
var backupFolder = DriveApp.getFolderById(backupFolderId);

while(latestFiles.hasNext()) {
 var file = latestFiles.next();
 file.makeCopy(backupFolder);
}

It works fine - copying all files from the source folder to the backup folder- except that if any file being copied is a "Google Apps script" file it doesn't copy it to the backup folder, it copies it to "My Drive".

I then have to move it from "My Drive" to the required folder. I can do this with a script but I can't work out why it is doing this in the first place.

I am the owner of all the files and folders.

Any ideas why?

thanks


Solution

  • Also in my environment, I confirmed the same situation with you. Only Google Apps Scripts cannot be copied to the backup folder. I think that this may be a bug. So I thought of the workaround for this situation. The patterns I tested are as follows.

    1. Tried to move the copied GAS files from "My Drive" to the backup folder using addFile() and removeFile().
      • A parent ID of backup folder can be added. But the parent ID of "My Drive" cannot be removed.
        • Failure
    2. Tried to copy GAS files using Drive.Files.copy of Advanced Google Services.
      • The copied GAS files are created to "My Drive". This is the same to makeCopy().
        • Failure
    3. Tried to move the copied GAS files from "My Drive" to the backup folder using Drive.Files.update of Advanced Google Services.
      • The parent ID can be changed from "My Drive" to backup folder.
        • Success

    Modified script :

    In this modified script, Google Apps Script files are moved from "My Drive" to backup folder using Drive.Files.update.

    var sourceFolderId = "xxxxxx";
    var sourceFolder = DriveApp.getFolderById(sourceFolderId);
    var latestFiles = sourceFolder.getFiles();
    var backupFolderId = "yyyyyy";
    var backupFolder = DriveApp.getFolderById(backupFolderId);
    
    while(latestFiles.hasNext()) {
      var file = latestFiles.next();
      var res = file.makeCopy(backupFolder); // Modified
      if (file.getMimeType() == MimeType.GOOGLE_APPS_SCRIPT) { // Added
        Drive.Files.update({"parents": [{"id": backupFolderId}]}, res.getId()); // Added
      }
    }
    

    Note :

    In order to use this modified script, please enable Drive API at Advanced Google Services and API console as follows.

    Enable Drive API v2 at Advanced Google Services

    • On script editor
      • Resources -> Advanced Google Services
      • Turn on Drive API v2

    Enable Drive API at API console

    • On script editor
      • Resources -> Cloud Platform project
      • View API console
      • At Getting started, click Enable APIs and get credentials like keys.
      • At left side, click Library.
      • At Search for APIs & services, input "Drive". And click Drive API.
      • Click Enable button.
      • If API has already been enabled, please don't turn off.

    References :

    If I misunderstand your question, I'm sorry.