0
votes

I am new to Google Apps Script. I'm trying to get for our workplace a script to move files from a single folder on a Google My Drive, over to a Team Drive if part of the filename has a certain phrase. However the script is still moving any and all files from the My Drive source folder to the Team Drive target folder, even those that don't have the "required" phrase in the file name. Example file names used:

ALT ADJ 01_03_2018

ALT CHG 01_03_2018

ALT CHG 01_04_2018

ALT PMT 01_03_2018

APX ADJ 01_03_2018

Enabled Drive API v2 in Advanced Google Services under Resources

function moveFileToFolder() { 
  var upldFldr=DriveApp.getFolderById('<<original Folder ID>>');
  var files=upldFldr.getFiles();
  supportsTeamDrives: true;
  while(files.hasNext()) {
    var file=files.next();
    var key=file.getName().slice(0,7);  //intended to take the first 7 characters of the filename.
      if (key = "ALT CHG") {
        supportsTeamDrives: true;
        supportTeamDrives: true;
        var targetFolder = DriveApp.getFolderById('<<new folder ID>>');
        targetFolder.addFile(file);
      };
  }
}

In the code, I'm trying to get only files that have the first 7 letters of ALT CHG in the file name to be moved/copied to the Team Drive folder. Instead, all of the files end up in the Team Drive Folder. Note this is the entire code start to finish.

1

1 Answers

1
votes

Change:

if (key = "ALT CHG") {

to

if (key == "ALT CHG") {

This should fix the issue, i.e. use == instead of =