enter image description here请参考所附的屏幕截图。 enter image description here 我想将文件移动到具有匹配名称的文件夹。 在这里,如果您看到文件(ABC1和testABC2)应类似地移至文件夹ABC,则文件123xyz应该移至文件夹xyz。 伙计们,请给我建议正确的方法和脚本。
`function MoveFiles(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
var ssid = ss.getId();
var ssparents = DriveApp.getFileById(ssid).getParents().next();
Logger.log('1.parent folder-'+ssparents);
// var files = ssparents.getFiles();
//-------------------------
var folderIterator = ssparents.getFolders();
for (var k =1; folderIterator.hasNext(); k++)
{
var folder = folderIterator.next().toString();
Logger.log('----------------------------------------- ');
Logger.log('------FOLDER NAME USING FOR LOOP. '+k+' - '+folder);
var filesIterator = ssparents.getFiles();
for ( var i = 0;filesIterator.hasNext();i++) /*to check files in parent folder */
{
// Logger.log('2.'+i+' - '+file4+ ' checking for folder- '+ folder);
// var param= " title contains '"+folder+"' ";
var checkFileName = ssparents.searchFiles(" title contains '"+folder+"' ");
for (var j=0; checkFileName.hasNext();j++)
{
var file2 = checkFileName.next().getName();
var check= filesIterator.next().toString();
Logger.log(check+'-------'+file2+'-value of checkFileName '+folder);
}
}
}
/* // addding files to other folder and removing fromthe base folder
// row.toString().match(item) == item //file.toString().match(ssparents)
var destination = DriveApp.getFolderById('1U8_0NT4h1QRy00eJHAFaH9mKmsVptaz9');
Logger.log('4. check - '+ destination)
destination.addFile(file);
var pull = DriveApp.getRootFolder();
pull.removeFile(
file);
enter code here
}
*/
SpreadsheetApp.flush();
}`
[1]: https://i.stack.imgur.com/AmrbM.png
答案 0 :(得分:0)
contains运算符仅对标题执行前缀匹配。对于 例如,标题“ HelloWorld”将匹配标题包含“ Hello”的标题 但标题不包含“世界”。
testABC2
来检索诸如searchFiles("title contains 'ABC' ")
之类的文件WORKAROUND
基于您的脚本的示例代码段:
function MoveFiles(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
var ssid = ss.getId();
var ssparents = DriveApp.getFileById(ssid).getParents().next();
Logger.log('1.parent folder-'+ssparents);
var folderIterator = ssparents.getFolders();
while (folderIterator.hasNext())
{
var folder = folderIterator.next();
Logger.log("folder name: '"+folder.toString()+"' ");
var filesIterator = ssparents.getFiles();
while (filesIterator.hasNext()) //to check files in parent folder
{
var file=filesIterator.next();
if (file.getName().indexOf(folder) > -1) {
Logger.log("file "+file.getName()+" found");
folder.addFile(file);
ssparents.removeFile(file);
}
}
}
}