0

I want to rename the files inside my google drive folder using google script.

I want to replace oldname with my explicitly mentioned new names individually

For the purpose of this question , i am making it simple to understand

Here's the folder id - 170hvRkGqyeIHsRMjjfLphv_Js2vIb5gV

There are 3 files inside it named 1.mp4 , 2.mp4 , 3.mp4 ... I want to rename them to abc.mp4 , xyz.mp4 , pqr.mp4

The javascript should function like

search 1.mp4 in given folder id recursively , if found replace with abc.mp4 ,else ignore . Search 2.mp4 in given folder id recursively , if found replace with xyz.mp4 , else ignore . Search 3.mp4 in given folder id recursively , if found replace with pqr.mp4 , else ignore .

( Search recursively because just to simplify and test any answers here quickly i have kept 3 files , but in real usage there are subfolders present )

As someone here suggested to use if/else statements , i tried doing so. Here's the script i used , for now i am just trying to change 1.mp4 to abc.mp4 as a quick mini test. Running the code doesn't give me any error but it doesn't change the filename at all . During script runtime it even did ask for my drive access but still failed to complete the job .

function rename() {
 console.log('Script Working');
 var SourceFolder = DriveApp.getFolderById("170hvRkGqyeIHsRMjjfLphv_Js2vIb5gV");
 console.log(SourceFolder);
 var Files = SourceFolder.getFiles();
 console.log(Files);
 for (var i in Files) {
  if (i.getName().equals("1.mp4")) {
   console.log(i);
   i.setName("abc.mp4");
   console.log(i);
  }
 }
}

rename();

I have added console log in script to know where exactly its going wrong , here's the console log

[20-06-18 20:08:38:913 IST] Script Working
[20-06-18 20:08:38:991 IST] {}
[20-06-18 20:08:38:993 IST] {}
[20-06-18 20:08:39:004 IST] Script Working
[20-06-18 20:08:39:081 IST] {}
[20-06-18 20:08:39:085 IST] {}

I don't understand why its unable to fetch the source folder

Rubén
  • 34,714
  • 9
  • 70
  • 166
Sachin
  • 1,217
  • 2
  • 11
  • 31

1 Answers1

2

You could also do it this way which is close to your original attempt but instead uses the iterator that is returned from the getFiles() method:

function rename(iA=['FileA','FileB','FileC'],oA=['File1','File2','File3']) {
  const SourceFolder=DriveApp.getFolderById("Folder Id");
  const Files = SourceFolder.getFiles();
  while(Files.hasNext()) {
    let file=Files.next();
    let idx=iA.indexOf(file.getName());
    if(idx!=-1) {
      file.setName(oA[idx])
    }
  }
}

I modified this slight to use ES6 syntax a bit more. But I wanted to test it before doing so. I would probably want to modify it even more so that I could input the new and old names just as strings and be able to just supply one. But I felt that learning how to use the iterator was more valuable to you than having a good general purpose function. I'll leave that up to you.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • You are a life saver man , after struggling for more than 10 hours on this , finally this works perfect. Thanks a ton. On a side note. Just edit the 3rd line .mpg to .mp4 ( small typo ) – Sachin Jun 18 '20 at 15:42
  • Also just out of curiosity , the search in sourcefolder will be done only for files directly inside the source folder id or recursively for files in subfolders too ? – Sachin Jun 18 '20 at 15:43
  • 1
    It does not recurse into sub folders. Here's an example of a recurse function which could be modified to accomplish that desire: https://stackoverflow.com/a/55248127/7215091 – Cooper Jun 18 '20 at 15:47