Christian
Christian

Reputation: 21

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

}

Upvotes: 0

Views: 125

Answers (2)

Anton Dementiev
Anton Dementiev

Reputation: 5706

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

Upvotes: 0

Maxime Munger
Maxime Munger

Reputation: 86

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

Upvotes: 2

Related Questions