- You want to copy a file with the filename of
Test.txt in the source folder to the destination folder by changing the filename.
- You want to copy the file using the filename.
- You want to achieve this using Google Apps Script.
If my understanding is correct, how about this modification?
Modification points:
- In order to retrieve the file using the filename, please use the method of
getFilesByName(). In this case, "FileIterator" is returned, because the files of same filename can be existing at Google Drive.
- When the filename is used for
getFileById(fileId), an error occurs.
- Using this, in order to retrieve the file in the specific folder, please modify to
source_folder.getFilesByName(filename).
- When the file is copied, you can use the method of makeCopy(name, destination). By this, the file can be directly copied to the destination folder.
When your script is modified, it becomes as follows.
Pattern 1:
In this pattern, the folder ID is used.
Modified script:
function copyfile() {
var filename = "Test.txt";
var sourceFolderId = "0B8_ub-Gf21e-fkxjSUwtczJGb3picl9LUVVPbnV6Vy1aRFRWc21IVjRkRjBPTV9xMWJLRFU";
var destinationFolderId = "0B8_ub-Gf21e-flJ4VmxvaWxmM2NpZHFyWWxRejE5Y09CRWdIZDhDQzBmU2JnZnhyMTU2ZHM";
var source_folder = DriveApp.getFolderById(sourceFolderId);
var file = source_folder.getFilesByName(filename);
if (file.hasNext()) {
var dest_folder = DriveApp.getFolderById(destinationFolderId);
var srcFile = file.next();
var newName = 'BACKUP ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '.' + srcFile.getName();
srcFile.makeCopy(newName, dest_folder);
}
}
- When you run the script, if there is the file of the filename of
Test.txt in the source folder, the file is copied to the destination folder.
Pattern 2:
In this pattern, the folder name is used.
Modified script:
function copyfile() {
var filename = "Test.txt";
var sourceFolderName = "Folder 1";
var destinationFolderName = "Folder 2";
var source_folder = DriveApp.getFoldersByName(sourceFolderName).next();
var file = source_folder.getFilesByName(filename);
if (file.hasNext()) {
var dest_folder = DriveApp.getFoldersByName(destinationFolderName).next();
var srcFile = file.next();
var newName = 'BACKUP ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '.' + srcFile.getName();
srcFile.makeCopy(newName, dest_folder);
}
}
- In this case, it supposes that the folders of
Folder 1 and Folder 2 are existing in the Google Drive.
References:
If I misunderstood your question and this was not the direction you want, I apologize.
Edit:
- You want to copy two files of "Test.txt" and "Test2.txt" in the folder
Folder 1.
For this situation, how about the following script?
Sample script 1:
function copyfile() {
var filenames = ["Test.txt", "Test2.txt"];
var sourceFolderName = "Folder 1";
var destinationFolderName = "Folder 2";
var source_folder = DriveApp.getFoldersByName(sourceFolderName).next();
for (var i = 0; i < filenames.length; i++) {
var filename = filenames[i];
var file = source_folder.getFilesByName(filename);
if (file.hasNext()) {
var dest_folder = DriveApp.getFoldersByName(destinationFolderName).next();
var srcFile = file.next();
var newName = 'BACKUP ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '.' + srcFile.getName();
srcFile.makeCopy(newName, dest_folder);
}
}
}
- If you want to add the filenames, please add them to the array of
filenames.
Sample script 2:
If you want to copy all files in the folder of Folder 1 to the folder of Folder 2, you can use the following script.
function copyfile() {
var sourceFolderName = "Folder 1";
var destinationFolderName = "Folder 2";
var source_folder = DriveApp.getFoldersByName(sourceFolderName).next();
var files = source_folder.getFiles();
var dest_folder = DriveApp.getFoldersByName(destinationFolderName).next();
while (files.hasNext()) {
var srcFile = files.next();
var newName = 'BACKUP ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '.' + srcFile.getName();
srcFile.makeCopy(newName, dest_folder);
}
}