Streamline Your Workflow: Automating Google Drive Organization with Google Apps Script
Let’s say goodbye to messy Google Drive folders! This tutorial will walk you through automating your Google Drive organization using Google Apps Script. We’ll create a custom script that sorts files based on specific criteria, such as file type or the date they were last modified. Ready to clean up your digital space? Let’s get to it!
Before We Begin
Ensure you have:
- A Google Account (for Google Drive and Google Apps Script)
- Familiarity with basic JavaScript (Google Apps Script is based on JavaScript)
Step 1: Accessing Google Apps Script
We’ll start by opening Google Apps Script.
1. Open your Google Drive.
2. Click on 'New' > 'More' > 'Google Apps Script'.
This will launch the Apps Script editor in a new tab.
Step 2: Writing Your Script
Let’s create a script that organizes files in a specific Google Drive folder based on their type.
function sortFilesInFolder() {
var folderId = 'your-folder-id'; // Replace with your folder ID
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var fileType = file.getMimeType();
// Create or access the subfolder for this file type
var subfolder;
try {
subfolder = folder.getFoldersByName(fileType).next();
} catch (e) {
subfolder = folder.createFolder(fileType);
}
// Move the file to the correct subfolder
subfolder.addFile(file);
folder.removeFile(file);
}
}
What’s happening in this script? Here’s a rundown:
DriveApp.getFolderById(folderId);
retrieves the folder we want to organize.folder.getFiles();
gets all the files in this folder.- We then loop over each file, getting its MIME type (i.e., file type).
- The script checks if a subfolder for this file type already exists. If not, it creates one.
- Finally, it moves the file to the corresponding subfolder.
Ensure you replace 'your-folder-id'
with the ID of the folder you want to sort.
Step 3: Running Your Script
Now that we have our script, let’s put it to work.
1. Click the disk icon or 'File' > 'Save' to save your script. Name it 'SortFiles'.
2. To run the script, select the 'SortFiles' function in the dropdown menu, then click the play button.
Google will ask for permissions to manage your files in Google Drive. Grant these permissions to let your script run.
And voila! Your Google Drive files are now sorted into separate subfolders based on their file type. You can modify this script as needed to sort based on different criteria, like last modified date or file size. Now you have a clean, organized Google Drive and a new tool in your automation toolkit. Happy organizing!