使用文件名将一个文件夹复制到Google云端硬盘中的另一个文件夹 [英] Copy File One folder to another folder in Google Drive using file name
问题描述
我在Google云端硬盘帐户中有两个文件夹。 文件夹1和文件夹2。 文件夹1中的
有多个文件。可以说有一个文件名 Test.txt
我想将 Test.txt
文件复制到文件夹2使用Driveapp。我找到了代码,但是它只能与文件唯一ID一起使用,反正我们只能按文件名来创建它吗?
function copyfile(){
var file = DriveApp.getFileById( 1pkwQ9te-EtpqC_NC3BoHzOTUoC7axZDcAfxrqMgslwg);
变种SOURCE_FOLDER = DriveApp.getFolderById( 0B8_ub-Gf21e-fkxjSUwtczJGb3picl9LUVVPbnV6Vy1aRFRWc21IVjRkRjBPTV9xMWJLRFU)
变种DEST_FOLDER = DriveApp.getFolderById( 0B8_ub-Gf21e-flJ4VmxvaWxmM2NpZHFyWWxRejE5Y09CRWdIZDhDQzBmU2JnZnhyMTU2ZHM)
//制作备份副本。
var file2 = file.makeCopy('BACKUP'+ Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'yyyy-MM-dd')+'。'+ file.getName ());
dest_folder.addFile(file2);
source_folder.removeFile(file2);
}
如果我使用文件名而不是File Unqiue ID,则会出现错误:
TypeError:在对象FileIterator中找不到函数makeCopy。
- 您要复制文件名为
Test.txt
的文件通过更改文件名将源文件夹复制到目标文件夹。 - 您要使用文件名复制文件。
- 您要使用以下方法实现此目的Google Apps脚本。
如果我的理解是正确的,那么该修改如何?
修改点:
- 要使用文件名检索文件,请使用
getFilesByName方法()
。在这种情况下,将返回 FileIterator,因为相同文件名的文件可以存在于Google云端硬盘中。
- 文件名用于
getFileById(fileId)
时,会发生错误。 - 使用此方法,为了检索特定文件夹中的文件,请修改为
source_folder.getFilesByName(filename)
。
- 文件名用于
- 复制文件后,可以使用 makeCopy(名称,目标)。这样,可以将文件直接复制到目标文件夹。
修改脚本后,它如下所示。 p>
模式1:
在此模式下,使用文件夹ID。
修改后的脚本:
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);
}
}
- 运行脚本,如果源文件夹中有文件名
Test.txt
的文件,则该文件将复制到目标文件夹。
模式2:
在此模式下,使用文件夹名称。
修改后的脚本:
function copyfile(){
var filename = Test.txt ;
var sourceFolderName =文件夹1;
var destinationFolderName =文件夹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);
}
}
- 在这种情况下,它假定
文件夹1
和文件夹2
的文件夹已存在于Google云端硬盘中。
参考文献:
- getFilesByName(name)
- Class FileIterator
- < a href = https://developers.google.com/apps-script/reference/drive/file#makecopyname,-destination rel = nofollow noreferrer> makeCopy(名称,目的地)
- getFoldersByName(name)
- 类FolderIterator
如果我误解了您的问题,而这不是您的方向
编辑:
- 您要复制两个文件
文件夹1
中的 Test.txt和 Test2.txt。
在这种情况下,以下脚本如何?
示例脚本1:
function copyfile(){
var filenames = [ Test.txt, Test2.txt];
var sourceFolderName =文件夹1;
var destinationFolderName =文件夹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);
}
}
}
- 如果要添加文件名,请将其添加到
文件名
的数组中。
示例脚本2:
如果要将文件夹1
文件夹中的所有文件复制到文件夹2
的文件夹,可以使用以下脚本。
函数copyfile(){
var sourceFolderName =文件夹1;
var destinationFolderName =文件夹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);
}
}
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. "
- 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 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.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
andFolder 2
are 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);
}
}
这篇关于使用文件名将一个文件夹复制到Google云端硬盘中的另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!