简体   繁体   中英

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

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().

Finally, you should remove files.next() from the beginning of line 7, as this chaining is incorrect and is likely causing your error!

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. 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).

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.

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);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM