How to Add #Tags to Files in Google Drive

Google Drive offers folders (or collections) for organizing your files but doesn’t support labels or tags. A disadvantage with folders is that a file can only belong to a single folder while multiple #tags, as in Gmail, can be assigned to a single file.

There’s a workaround though. You can assign tags to a file in Google Drive by adding them to the description of a file. A Google forum user has written this Google Script that takes the folder name in which a file is found and assigns those names as tags to the file. It works for nested folders too.

/*
 setDescriptionToFolderNames
 Workaround to fake Tags in Google Drive.
 Writes all the folder names of a file into the file description, so that the file can be found by searching the folder names.
*/

function setDescriptionToFolderNames() {
  var file;
  var filename;
  var folders;
  var filedescription;

  var contents = DocsList.getAllFiles();
  // sort ascending. Oldest first, in case of timeout:
  contents.sort(function (a, b) {
    return a.getLastUpdated() - b.getLastUpdated();
  });

  // synchronize folder names of all files (only updates if folders have changed):
  for (var i = 0; i < contents.length; i++) {
    file = contents[i];
    try {
      filename = file.getName();
      //Logger.log("Checking: " +filename +" ("+file.getLastUpdated()+")");
      folders = file.getParents();

      // sort by folder name:
      folders.sort(function (a, b) {
        return a.getName().localeCompare(b.getName());
      });
      filedescription = '';

      for (var f = 0; f < folders.length; f++) {
        filedescription = filedescription + folders[f].getName() + ' ';
      }

      if (filedescription != contents[i].getDescription()) {
        file.setDescription(filedescription);
        Logger.log('Updated: ' + filename);
      }
    } catch (e) {
      Logger.log('Error: ' + filename + ' ' + e);
    }
  }
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻