我有四个文件夹 主文件夹 资料夹1 资料夹2 文字文件夹
我想将一些文件从Main Folder
复制到Folder 1
和Folder 2
Text Folder
中有两个文本文件,可以说Folder 1.txt
和Folder 2.txt
screnshot https://imgur.com/a/0Ov8RRq,每个文件都包含Main Folder
内部可用的文件名。现在我想要脚本,该脚本将从Folder 1.txt
获取文件列表名称并复制到Folder 1
也从Folder 2.txt
复制文件列表名称并复制到Folder 2
这是我当前的工作代码
function copyfile(obj) {
var source_folder = DriveApp.getFoldersByName(obj.sourceFolderName).next();
var file = source_folder.getFilesByName(obj.filename);
if (file.hasNext()) {
var dest_folder = DriveApp.getFolderById(obj.destinationFolderId);
var srcFile = file.next();
var newName = srcFile.getName();
srcFile.makeCopy(newName, dest_folder);
}
}
// Please run this function.
function main() {
// Please set this object.
var object = [
{filename: "filenames_value1", sourceFolderName: "sourceFolderName_value1", destinationFolderId: "destinationFolderId _value1"},
{filename: "filenames_value2", sourceFolderName: "sourceFolderName_value2", destinationFolderId: "destinationFolderId _value2"}
];
for (var i = 0; i < object.length; i++) {
copyfile(object[i]);
}
}
答案 0 :(得分:2)
将文件复制到多个目标位置
function copyFiles() {
var fA=[];
var mainFolder=DriveApp.getFoldersByName('Main Folder').next();//assume one
var textFolder=DriveApp.getFoldersByName('Text Folder').next();//assume one
var files=textFolder.getFiles();
while(files.hasNext()) {
var file=files.next();
var filename=file.getName();
if(filename.match(/Folder\d{1,}\.txt/)) {
var desFldrName="Folder" + filename.slice(6,-4)
var fnA=file.getBlob().getDataAsString().split('\n');
for(var i=0;i<fnA.length;i++) {
fA.push({name:fnA[i],srcFolderId:mainFolder.getId(),desFolderName:desFldrName});
}
}
}
for(var i=0;i<fA.length;i++) {
var srcFolder=DriveApp.getFolderById(fA[i].srcFolderId);
var desFolder=DriveApp.getFoldersByName(fA[i].desFolderName).next();//assume one
var fi=srcFolder.getFilesByName(fA[i].name).next();//assume one
fi.makeCopy(fi.getName(),desFolder);
}
}
在运行脚本之前,所有目标文件夹都为空,下图显示了复制结果。还显示了控制复制过程的txt文件的内容。