0
votes

I have files in google drive in upper case, I want to rename them all to lower case

Example :

ZVXDA0002-ZPOILTY024(full).xml should be renamed as zvxda002-zpoilty024(full).xml

here is the script I tried

function FileRenaming() {
  var SourceFolder = DriveApp.getFolderById("Test_rename")
  var Files = SourceFolder.getFiles();
  while(Files.hasNext()) {
    var file = Files.next();
    var FileRename =  file.makeCopy(file.getName());
    var DestinationFolder = DriveApp.getFolderById("Test_renameD")
    DestinationFolder.addFile(FileRename);
    SourceFolder.removeFile(file);
}
}

returns

Exception: Unexpected error while getting the method or property getFolderById on object DriveApp. (line 2, file "Code")Dismiss
2

2 Answers

2
votes

DriveApp.getFolderById() expects an Id, not a name

  • If you do not know the Id of the folder, you can use DriveApp.getFoldersByName(name) instead
  • Mind in this case that you will get in this case an array of potentially several folders with the same name (since names unlike Ids are not unique on Google Drive)
  • You need to use DriveApp.getFoldersByName(name).next() to retrieve the first folder with the given specified name
  • As for renaming the folder, you simply need to use the function setName(name)
  • The script you provided does not change the name but instead makes a copy of the file into different folder

Sample how to rename a folder:

function renameFolder(){
 var file = DriveApp.getFoldersByName(name).next();
 var oldName = file.getName();
 var newName = oldName.toLowerCase();
 file.setName(newName)
}

Sample how to rename files in a folder:

function renameFiles(){
 var folder = DriveApp.getFoldersByName(name).next();
 var Files = folder.getFiles();
 while(Files.hasNext()) {
   var file = Files.next();
   var oldName = file.getName();
   var newName = oldName.toLowerCase();
   file.setName(newName)
 }
}
0
votes

If you create a follow up spreadsheet, and assuming that all files you wish to rename are within a specific folder, the following code will help you to list the files (this is optional in the script but could be useful to double check script is doing its job!!) and then rename the files to lowercase. You can either run the script from the menu created (My Files) within the spreadsheet or set a trigger to run it automatically at a specific times/days:

So, you need:

CODE:

     // ---------- Menu ----------
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('My Files')
  .addItem('Rename files', 'RenameFilesToLowerCase')
  .addToUi();
}



function RenameFilesToLowerCase() {

  var folder = DriveApp.getFolderById('ID GOES HERE');  
  var sheetId = "ID GOES HERE";
  var ss = SpreadsheetApp.openById(sheetId);

  // specify the sheet to write the DATA, here it's Sheet1

  var sheet = ss.getSheetByName("Sheet1"); 
  sheet.getRange("A2:B").clear();

  var output = [];
  var file;
  var name;
  var link;
  var contents = folder.getFiles();
  while(contents.hasNext()) {
    file = contents.next();    
    data = [
      file.setName(file.getName().toLowerCase()),
      name =  file.getName(),
      link =  file.getUrl(),
    ];

    output.push([name, link]);

    // write data to the sheet  
    sheet.getRange(2, 1, output.length, output[0].length).setValues(output);  
  }  
};