简体   繁体   English

在Google云端硬盘的Google App脚本中,如果某些文件的标题包含特定的单词,如何将它们从根目录移至特定的文件夹?

[英]In Google-App-Script for Google Drive how do I move certain files from root to a certain folder if their title includes a certain word?

I want to use Google-App-Script to move certain files into a specific folder if their title contains a certain word 我想使用Google-App-Script将某些文件移动到特定文件夹(如果它们的标题包含特定单词)

I have tried something like this but the error message states that the method is not defined. 我已经尝试过类似的操作,但是错误消息指出该方法未定义。 Any pointers/suggestions? 有任何指示/建议吗?

This is my code: 这是我的代码:

function myFunction() {
    var searchFor ='title contains "Copyright"';
    var names =[];
    var files = DriveApp.searchFiles(searchFor);
    var destination = DriveApp.getFolderById("1xN0AUclE2t9yqlNzTwgRup3nn7G0Qsik");
    while (files.hasNext()) {
        files.next().destination.addFile(file);
}

} }

It looks like you did not define the "file" variable anywhere in your code. 看来您没有在代码中的任何地方定义“文件”变量。

You can either define it in your while loop, or pass files.next() as an argument to addFile(). 您可以在while循环中定义它,也可以将files.next()作为addFile()的参数传递。

Finally, you should remove files.next() from the beginning of line 7, as this chaining is incorrect and is likely causing your error! 最后,您应该从第7行的开头删除files.next(),因为此链接不正确,可能会导致您的错误!

Also, note that the file will not be removed from its original folder, but will be accessible from both locations. 另外,请注意,该文件不会从其原始文件夹中删除,但可以从两个位置访问。 If you wish to remove the file from the original location, let me know and I will edit my code to add this. 如果您想从原始位置删除文件,请告诉我,我将编辑代码以添加此文件。

Try this: 尝试这个:

function myFunction() {
  var searchFor ='title contains "Copyright"';
  var names =[];
  var files = DriveApp.searchFiles(searchFor);
  var destination = DriveApp.getFolderById("1xN0AUclE2t9yqlNzTwgRup3nn7G0Qsik");
  while (files.hasNext()) {
    var file = files.next();
    destination.addFile(file);
  }
}

or this: 或这个:

function myFunction() {
  var searchFor ='title contains "Copyright"';
  var names =[];
  var files = DriveApp.searchFiles(searchFor);
  var destination = DriveApp.getFolderById("1xN0AUclE2t9yqlNzTwgRup3nn7G0Qsik");
  while (files.hasNext()) {
    destination.addFile(files.next());
  }
}

Note that your code doesn't do what you say it does. 请注意,您的代码并没有像您所说的那样执行。 Namely, it doesn't actually 'move' files from the root folder to the specified folder (if that's what you want to accomplish). 也就是说,它实际上并没有将文件从根文件夹“移动”到指定的文件夹(如果要完成此操作)。

In Google Drive, folders and files can have more than one parent. 在Google云端硬盘中,文件夹和文件可以有多个父对象。 Think of folders as 'labels' applied to files and other folders. 将文件夹视为应用于文件和其他文件夹的“标签”。 Calling the addFile(file) method of the Folder class will add the file to the new folder but that same file will also remain in its original parent folder(s). 调用Folder类的addFile(file)方法会将文件添加到新文件夹,但是相同的文件也将保留在其原始父文件夹中。

This could cause problems as you may accidentally remove the file from one of the folders thinking it's a copy when in fact it's the same file stored in multiple folders. 这可能会导致问题,因为您可能会意外地从一个文件夹中删除该文件,并认为它实际上是一个副本,而实际上它是存储在多个文件夹中的同一文件。

Since there's no direct way to 'move' the file to another folder in Google Drive, the actual process consists of 2 steps. 由于无法直接将文件“移动”到Google云端硬盘中的另一个文件夹,因此实际过程包括2个步骤。

Getting the list of parent folders for the file: 获取文件的父文件夹列表:

var folderIterator = file.getParents();   

Adding the file to the destination folder and removing it from the previously saved parent folders 将文件添加到目标文件夹并将其从以前保存的父文件夹中删除

targetFolder.addFile(file);

//Remove from parents
while (folderIterator.hasNext()) {    
 folderIterator.next().removeFile(file);  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google脚本将修订历史记录保留在Google云端硬盘中,以便在特定日期之后编辑文件? - How Can I Use A Google Script to Keep Revision History in Google Drive for Files Edited After A Certain Date? Google Apps脚本:如何从文件夹中删除某些文件类型 - Google Apps Script: How to trash certain file types from a folder 如何检查数组是否包含某些值 google Apps Script - How to check whether array includes certain value google Apps Script 将某种类型的文件添加到我的Google云端硬盘 - Adding Files Of A Certain Type To My Google Drive 如何使用共享的Google驱动器文件夹上的Google App脚本删除特定文件夹中的特定类型的文件(CSV文件) - How do I delete a specific type of file (CSV files) within a specific folder with google app script on a shared google drive folder Google应用程序脚本如何将多个文件移动到文件夹中? - Google app scripts How do I move several files into a folder? 如何使用 Google Script 从本地 Google Drive 中的图像文件夹创建 Google 幻灯片演示文稿? - How do I create a Google Slides presentation from a folder of images in a local Google Drive using Google Script? 如何结合两个功能google-app-script? - How to combine two functions google-app-script? 使用Google App脚本将文件夹中的所有文件移动到Google驱动器中的其他文件 - Move all files in a folder to other in google drive using google app script google-app-script 如何将所有 blob 合并为一个 blob - google-app-script how to cocat all blob into one blob
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM