0

For 'reasons' management need me to rename a bunch of files in Google drive. When I say 'a bunch' I'm talking thousands. I'm scripting a way to do this.

So far I've managed to write some code that allows me to rename files within one level of a Folder Structure. Lets say the Folder Structure is:

\Folder 1
   \Folder 2
   \Folder 2.2
       \Folder 3

This code renames all the files in Folder 2 and Folder 2.2 (given the ID of Folder 1), but not in Folder 1 or Folder 3. I also don't know how many levels there are in the folder structure.

I think there should be a way I can have the file renaming bit of this code as a standalone function and call it for each folder under to top level folder. I just can't figure out how to do that.

What I have is as follows:

function renameFile() {
 var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Main");
  var lastRow = sourceSheet.getLastRow();
  var filePaths = sourceSheet.getRange(4, 1, 1, 1).getValues(); 

  var pre = sourceSheet.getRange("C4").getValue(); 
  var folder = DriveApp.getFolderById(filePaths);

  var contents = folder.getFiles();
  var folders = folder.getFolders();
  var ver = 1;

  while (folders.hasNext()) {
    var subFolder = folders.next();
    var subFolderID = subFolder.getId();
    var nextSubFolder = DriveApp.getFolderById(subFolderID);
    var subFiles = subFolder.getFiles();
    var subFolders = subFolder.getFolders();

    while (subFiles.hasNext()) {
      var file = subFiles.next();
      var curID = file.getId();
      var curName = file.getName();
      var fileToRename = DriveApp.getFileById(curID);

      var newName = pre + " " + curName + " Version " + ver;

      fileToRename.setName(newName);
    }
  }
}

Thanks in advance.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Dan Howard
  • 176
  • 2
  • 6
  • 16

1 Answers1

1
  • You want to rename all files under a specific folder.
    • You want to rename files from the original name to pre + " " + curName + " Version " + ver.
    • The values of pre and ver are constant.
  • There are various folder structures under the specific folder.

If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as one of them.

Modification points:

  • In your script, there are several variables which are not used in the script.
  • In your script, only files of the first level are retrieved.
    • For example, when a folder is retrieved, the folders in the retrieved folder are required to be retrieved recursively.

Modified script:

function renameFile() {
  // Added
  var renameFiles = function(id) {
    var files = DriveApp.getFolderById(id).getFiles();
    while (files.hasNext()) {
      var file = files.next();
      var curID = file.getId();
      var curName = file.getName();
      var newName = pre + " " + curName + " Version " + ver;
      file.setName(newName);
    }
    var ids = [];
    var folders = DriveApp.getFolderById(id).getFolders();
    while (folders.hasNext()) {
      ids.push(folders.next().getId());
    }
    if (ids.length > 0) ids.forEach(function(id) {renameFiles(id)});
  }

  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Main");
  var filePaths = sourceSheet.getRange(4, 1).getValue(); // Modified
  var pre = sourceSheet.getRange("C4").getValue(); 
  var ver = 1;
  renameFiles(filePaths); // Added
}

Note:

  • I recommend that before you run this script, test it using sample folder and files.
  • This script supposes as follows.
    • filePaths is the folder ID. Namely, the folder ID is put in a cell "A4".
  • If there are a lot of files in the folder, there is a possibility that the limitation of script runtime (6 min / execution) exceeds.
    • At that time, please try the follow methods.
      1. Divide the folder and run the script for each folder.
      2. At first, all files are retrieved. As the next step, it renames all filename using Batching Requests. Because "Batching Requests" works by the asynchronous processing, the proccess cost will be able to be reduced. I think that above modified script and this answer will be helpful for this.

Reference:

If I misunderstand your situation, please tell me. I would like to modify it.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 1
    It might be useful to have the script save old filename, new filename, id and possibly parent folder and rename date (possibly a script version number) in a spreadsheet. It might be useful information in the future. – Cooper Dec 11 '18 at 01:58
  • @Cooper Thank you for your comment. Also I think that your proposal will be useful for OP's situation. – Tanaike Dec 11 '18 at 03:49
  • Amazing Tanaike, thank you very much. I have a couple of questions about some lines in the code I'd like to know more about, if you could point me somewhere to aid my learning that would be immense. The first is this one: var renameFiles = function(id) What does the (id) bit mean, I can't work out what 'id' refers to? Is it passing a some data to the function? If so, what is that data? – Dan Howard Dec 11 '18 at 08:40
  • @Dan Howard Thank you for replying. I'm glad your issue was resolved. About ``id`` of ``var renameFiles = function(id) {}``, it's the folder ID. At first, when ``renameFiles(filePaths)`` is called, ``filePaths`` is given. This value is the folder ID you put. Then, ``id`` of ``function(id) {renameFiles(id)}`` in the function of ``renameFiles`` is used. By this, the next folder can be retrieved. If you want to study about the recursive function, you can search a lot of samples at Stackoverflow and other sites. – Tanaike Dec 11 '18 at 22:20