如何在G App Script中搜索文件名中的特定文本?我的完整文件名是"每日股票摘要报告Kadlunga20180426_061749_946.pdf"。我想用字符串"每日股票摘要报告Kadlunga"搜索所有文件。并将其复制到文件夹名称" Kadlunga"在G Drive。我当前的代码适用于完整的文件名。我不确定如何为p [文件名的艺术。
function moveFile() {
var dApp = DriveApp;
var folderIterator = dApp.getFoldersByName("Attachments");
var folder = folderIterator.next();
var filesIterator = folder.getFiles();
var Kadlunga = folder.getFoldersByName("Kadlunga").next();
var Napperby = folder.getFoldersByName("Napperby").next();
while (filesIterator.hasNext()){
var file = filesIterator.next();
var fileName = file.getName();
if(fileName == "Daily Stock Summary Report
Kadlunga20180426_061749_946.pdf")
{
file.makeCopy(Kadlunga);
}
if(fileName == "Daily Stock Summary Report
Napperby20180426_061749_958.pdf")
{
file.makeCopy(Napperby);
}
}
}
答案 0 :(得分:0)
这次修改怎么样?
searchFiles()
,检索文件名为Daily Stock Summary Report
的文件。indexOf()
,文件会被归类并导入Kadlunga
和Napperby
的文件夹。function moveFile() {
var dApp = DriveApp;
var folderIterator = dApp.getFoldersByName("Attachments");
var folder = folderIterator.next();
var filesIterator = folder.searchFiles('title contains "Daily Stock Summary Report"');
var Kadlunga = folder.getFoldersByName("Kadlunga").next();
var Napperby = folder.getFoldersByName("Napperby").next();
while (filesIterator.hasNext()) {
var file = filesIterator.next();
var fileName = file.getName();
if (fileName.indexOf("Kadlunga") > -1) {
file.makeCopy(Kadlunga);
}
if (fileName.indexOf("Napperby") > -1) {
file.makeCopy(Napperby);
}
}
}
如果我误解了你的问题,我很抱歉。