Home > front end >  I want to change the names of old files uploaded to Google Forms
I want to change the names of old files uploaded to Google Forms

Time:09-22

I have a google form and all the files uploaded by the participants contain random names.

  • The data is summarized in personal photos, ID photos, photos of certificates.
  • That is why we want to name the files with the names of the people themselves by choosing a specific field.
  • I tried in a code via the google apps script to change the names but it is no more than 70 to 80 files and then it stops
  • Is there a code that will help me with this work, especially since the number of files is large?
function renamefile() {
    var form = FormApp.openById('16aPNgaSuDZ-e0ibw08jW7_-YMULGWV-_iUP9uQHE'); //DDD
    var formResponses = form.getResponses();
    var baseString = 'https://drive.google.com/file/d/';
    var endString = '/view?usp=drivesdk';
        
    var folder = DriveApp.getFolderById('1BPA7wkECV1vOB6bo3tGfUZd9MifjwYJG2BVI_sHSeNojFY-xiN4dvLW_vc2zw4IEe');
    var files = folder.getFiles();

    while (files.hasNext()) {
        var file = files.next();
        for (var i = 0; i < formResponses.length; i  ) {
            var formResponse = formResponses[i];
            var itemResponses = formResponse.getItemResponses();
            var itemResponseFname = itemResponses[1];
            var itemResponsePhoto = itemResponses[15];
            var photoID = itemResponsePhoto.getResponse();
            var newName = itemResponseFname.getResponse()    " - "   "PHOTO"  ;
            var url = baseString   photoID   endString;
            var urlCheck = file.getUrl();
            
            if ( url == urlCheck) {
                var modName = newName   ".pdf" ;
                file.setName(modName);
            }
            
        }
    }
} 

CodePudding user response:

Modification points:

  • From your showing script, it seems that the form responses are retrieved every loop of the "while loop". I thought that this might be a high process cost.
  • From your script, it seems that you want to rename PDF files. In this case, when PDF files are retrieved. The process cost might be able to be reduced.
  • I thought that in your situation, the file ID can be used for checking the file instead of URL.

When these points are reflected in your script, how about the following modification?

Modified script:

function renamefile() {
  var form = FormApp.openById('###'); // Please set your file ID of Google Forms.
  var folder = DriveApp.getFolderById('###'); // Please set your folder ID.

  // Create an object for searching file ID.
  var obj = form.getResponses().reduce((o, f) => {
    var itemResponses = f.getItemResponses();
    var itemResponseFname = itemResponses[1];
    var itemResponsePhoto = itemResponses[15];
    var fileId = itemResponsePhoto.getResponse();
    var newName = itemResponseFname.getResponse()   " - "   "PHOTO";
    o[fileId] = newName   ".pdf";
    return o;
  }, {});

  // Rename files.
  var files = folder.getFilesByType(MimeType.PDF);
  while (files.hasNext()) {
    var file = files.next();
    var fileId = file.getId();
    if (obj[fileId]) {
      file.setName(obj[fileId]);
    }
  }
}
  • When this script is run, first, an object is created. This object is used for searching the file ID in the folder. And, using the object, the PDF files in the folder are renamed.

Note:

  • If your files are not PDF files, please modify var files = folder.getFilesByType(MimeType.PDF); to var files = folder.folder.getFiles();.

  • When the number of files is large and the processing time is over 6 minutes, I would like to propose using the batch requests. The sample script of batch request can be seen at this thread. And, when the above script is modified, the sample script is as follows. When you use this script, please enable Drive API at Advanced Google services. And also, please install a Google Apps Script library of BatchRequest. Ref

      function renamefile2() {
        var form = FormApp.openById('###'); // Please set your file ID of Google Forms.
        var folderId = "###"; // Please set your folder ID.
    
        // Create an object for searching file ID.
        var obj = form.getResponses().reduce((o, f) => {
          var itemResponses = f.getItemResponses();
          var itemResponseFname = itemResponses[1];
          var itemResponsePhoto = itemResponses[15];
          var fileId = itemResponsePhoto.getResponse();
          var newName = itemResponseFname.getResponse()   " - "   "PHOTO";
          o[fileId] = newName   ".pdf";
          return o;
        }, {});
    
        // Rename files using batch request.
        var { items } = Drive.Files.list({ q: `'${folderId}' in parents and trashed=false and mimeType='${MimeType.PDF}'`, maxResults: 1000, fields: "items(id)" });
        var requests = items.reduce((ar, { id }) => {
          if (obj[id]) {
            ar.push({
              method: "PATCH",
              endpoint: `https://www.googleapis.com/drive/v3/files/${id}`,
              requestBody: { name: obj[id] },
            });
          }
          return ar;
        }, []);
        var result = BatchRequest.EDo({ batchPath: "batch/drive/v3", requests });
      }
    
    • In this sample, it supposes that the number of your files is less than 1000. When the number of files is more than 1000, please retrieve all files using pageToken.

Reference:

  • Related