I am using Amit Agarwal's fantastic script to extract attachments from Google Mail and save them to Google Drive. It saves the attachment under its name and saves the Subject Line of the mail in the "Description" of the file (together with the Message ID). Instead, I would like to name the file whatever is written in the subject line of the email with the attachment.
An example: The attachment is named abc.pdf, the subject line is "Invoice-February.pdf". As it is, the script would create a file in Google Drive called abc.pdf. Instead, I would like it to name the file "Invoice-February.pdf".
This is the original code:
for (var x=0; x<threads.length; x++) {
var message = threads[x].getMessages()[0];
var desc = message.getSubject() + " #" + message.getId();
var att = message.getAttachments();
for (var z=0; z<att.length; z++) {
try {
if (check) {
var name = att[z].getName();
if (name.indexOf(".") != -1) {
var extn = name.substr(name.lastIndexOf(".")+1).toLowerCase();
if (valid.indexOf(extn) != -1) {
file = folder.createFile(att[z]);
file.setDescription(desc);
} else {
Logger.log("Skipping " + name);
}
}
} else {
file = folder.createFile(att[z]);
file.setDescription(desc);
}
}
The createFile function has no way to add a different name to a blog and I have not found a "renameFile". Does anyone know how to do that?