问题
I have two folder in google drive account. "Folder 1" and "Folder 2".
inside "Folder 1" there is multiple file. lets say there is a file name Test.txt I want to copy the Test.txt file to "Folder 2" using Driveapp. I found code but it works only with "File Unique ID" is there anyway we can make it by file name only?
function copyfile() {
var file = DriveApp.getFileById("1pkwQ9te-EtpqC_NC3BoHzOTUoC7axZDcAfxrqMgslwg");
var source_folder = DriveApp.getFolderById("0B8_ub-Gf21e-fkxjSUwtczJGb3picl9LUVVPbnV6Vy1aRFRWc21IVjRkRjBPTV9xMWJLRFU")
var dest_folder = DriveApp.getFolderById("0B8_ub-Gf21e-flJ4VmxvaWxmM2NpZHFyWWxRejE5Y09CRWdIZDhDQzBmU2JnZnhyMTU2ZHM")
// Make a backup copy.
var file2 = file.makeCopy('BACKUP ' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '.' + file.getName());
dest_folder.addFile(file2);
source_folder.removeFile(file2);
}
if i use file name instead of File Unqiue ID, i getting error:
"TypeError: Cannot find function makeCopy in object FileIterator. "
回答1:
- You want to copy a file with the filename of
Test.txtin 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 filename is used for
- 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.txtin 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 1andFolder 2are existing in the Google Drive.
References:
- getFilesByName(name)
- Class FileIterator
- makeCopy(name, destination)
- getFoldersByName(name)
- Class FolderIterator
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);
}
}
来源:https://stackoverflow.com/questions/58332942/copy-file-one-folder-to-another-folder-in-google-drive-using-file-name