Is it possible to run a script on subfolders

Solution 1:

I assume you mean you want it to run on all files in subfolders of the original folder. If you also want to run it on subfolders of those folders, and so on, you can use a recursive solution:

Use folder.getFolders() to find the subfolders, then run the rename function on those folders:

function rename(folderId) { // Add the parameter here

  // var folderid = "<<insert your folder ID>>" // Remove this line
  
  var folder = DriveApp.getFolderById(folderid)

  var myfiles = folder.getFiles();

  while (myfiles.hasNext()) { 
  
    var myfile = myfiles.next();   
    var myname = myfile.getName();
    var ftype = myfile.getMimeType();

    var indexOfFirst = ftype.indexOf(mimetype);


    var fname = myname.replace("source", "change");
    myfile.setName(fname);    
  } 

  // Add these lines to recursively apply the function to subfolders:
  var subfolders = folder.getFolders();
  while (subfolders.hasNext()) { 
    var subfolder = subfolders.next();
    rename(subfolder.getId()); // this calls the same function, but passes the subfolder id
  }

  return false;
}