This Google Apps Script automatically organizes files in your Google Drive based on their names. It moves files with specific naming conventions to designated folders.
- Automatically moves Google Docs, Sheets, Slides, and Forms to specified folders based on their titles.
- Easily configurable to add more folders and categories.
- A Google account to access Google Drive and Google Apps Script.
- Open Google Apps Script: Go to Google Apps Script.
- Create a New Project: Click on "New Project".
- Copy the Code: Use the provided code snippet below.
- Edit the Code:
- Replace placeholder folder names and IDs in the
folders
object with your actual folder names and IDs. - The folder ID can be found in the URL of the folder when opened in Google Drive.
- Replace placeholder folder names and IDs in the
- Set Up Time-Driven Triggers:
- Set a trigger to run this function periodically (e.g., every 10 or 15 minutes) to check for new files.
Here's the complete code for the file mover:
function moveFiles() {
const rootFolder = DriveApp.getRootFolder();
const folders = {
'Data 101': DriveApp.getFolderById('Folder ID here'), // Replace with actual folder ID
'Data 102': DriveApp.getFolderById('Folder ID here'), // Replace with actual folder ID
'Reports': DriveApp.getFolderById('Folder ID here'), // Replace with actual folder ID
// Add more folders as needed
};
const files = rootFolder.getFiles();
const movedFiles = [];
while (files.hasNext()) {
const file = files.next();
const title = file.getName();
const matches = title.match(/\[(.*?)\]/);
// Check if the file is a supported type
const mimeType = file.getMimeType();
const supportedTypes = [
MimeType.GOOGLE_DOCS,
MimeType.GOOGLE_SHEETS,
MimeType.GOOGLE_SLIDES,
MimeType.GOOGLE_FORMS,
];
if (matches && supportedTypes.includes(mimeType)) {
const category = matches[1].trim();
const targetFolder = folders[category];
if (targetFolder) {
targetFolder.addFile(file);
rootFolder.removeFile(file);
movedFiles.push(file.getName());
}
}
}
Logger.log('Moved files: ' + movedFiles.join(', '));
}
Suppose you have the following folders in your Google Drive:
Example folder structure in Google Drive.
- Data 101
- Data 102
- Reports
When naming your files, use the format:
*Example of file naming convention.*[Data 101] - Project Proposal
[Data 102] - Budget Report
[Reports] - Quarterly Review
- When a file named
[Data 101] - Project Proposal
is added to your root folder, the script will automatically move it to the Data 101 folder. - Similarly,
[Data 102] - Budget Report
will go to the Data 102 folder.
- Deploy the Script:
- Click on Deploy in the script editor.
- Choose New deployment.
- Select Time-driven triggers to run the
moveFiles
function at your desired intervals.
Steps for deploying the script.
- Set Up Triggers:
- Click on the clock icon (Triggers).
- Add a new trigger to run
moveFiles
based on your chosen frequency.
- Click the triggers (clock icon).
- Add a trigger.
- Select event source: Time-driven.
- Select type of time-based trigger: Hour timer.
- Select hour interval: Change to your liking.
- Adjust failure notification settings as needed.
- Ensure that the naming convention is consistent for the script to work correctly.
- The script only processes files of the specified types (Docs, Sheets, Slides, Forms).
This project is open-source and free to use. You can modify it as needed for your personal or organizational use.