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