Home > Enterprise >  Google drive archiving script with shared drives
Google drive archiving script with shared drives

Time:10-22

  • I have a script that automatically archives files if the filenames match, it looks for prefixes like 'ARCHIVE_BOM' and will then move that to its respective archive folder. I have gotten this to work on my personal drive but it won't work on shared drives:

    function moveFiles() {
      var dfldrs=['ARCHIVE_DRW','ARCHIVE_BOM','ARCHIVE_STP'];//Three letter prefixes
      var ofObj={ARCHIVE_DRW:'id',ARCHIVE_BOM:'id',ARCHIVE_STP:'id'};//distribution folder ids
      var upldFldr=DriveApp.getFolderById('id');
      var files=upldFldr.getFiles();
      while(files.hasNext()) {
        var file=files.next();
        var key=file.getName().slice(0,11);
        var index=dfldrs.indexOf(key);
        if(index>-1) {
          Drive.Files.update({"parents": [{"id": ofObj[key]}]}, file.getId());  
        }
        }
      }
    
  • I know that I need to add {supportsTeamDrives: true} somewhere but can't get it to work properly, if someone could help me place this it would be great.

CodePudding user response:

I think that in the current stage when the ID instead of the filename is used, DriveApp.getFolderById('id') can be used for the shared Drive. So, in this case, how about modifying Drive.Files.update({"parents": [{"id": ofObj[key]}]}, file.getId()); as follows?

From:

Drive.Files.update({"parents": [{"id": ofObj[key]}]}, file.getId());  

To:

Drive.Files.update({ "parents": [{ "id": ofObj[key] }] }, file.getId(), null, { supportsAllDrives: true });

or

Drive.Files.patch({ "parents": [{ "id": ofObj[key] }] }, file.getId(), { supportsAllDrives: true });
  • In the case of Drive.Files.update, please put { supportsAllDrives: true } to the 4th argument.

  • In the case of Drive.Files.patch, please put { supportsAllDrives: true } to the 3rd argument.

  • If in your situation, DriveApp.getFolderById('id') cannot be used for the shared Drive, please check the following modified script.

      function moveFiles2() {
        var dfldrs = ['ARCHIVE_DRW', 'ARCHIVE_BOM', 'ARCHIVE_STP'];
        var ofObj = { ARCHIVE_DRW: 'id', ARCHIVE_BOM: 'id', ARCHIVE_STP: 'id' };
    
        var { items } = Drive.Files.list({ q: `'id' in parents and trashed=false`, fields: "items(title,id)", maxResults: 1000, corpora: "allDrives", includeItemsFromAllDrives: true, supportsAllDrives: true });
        items.forEach(({ id, title }) => {
          var key = title.slice(0, 11);
          var index = dfldrs.indexOf(key);
          if (index > -1) {
            Drive.Files.update({ "parents": [{ "id": ofObj[key] }] }, id, null, { supportsAllDrives: true });
            // or Drive.Files.patch({ "parents": [{ "id": ofObj[key] }] }, id, { supportsAllDrives: true })
          }
        });
      }
    
    • In this case, it supposes that the number of files is less than 1000. If it is more than 1000, please use pageToken for retrieving all files.
  • As another modification, as a simple modification, how about the following modification?

    • From

        Drive.Files.update({ "parents": [{ "id": ofObj[key] }] }, file.getId());
      
    • To

        file.moveTo(DriveApp.getFolderById(ofObj[key]));
      

References:

  • Related