Inspiration
I often find myself importing data into google sheets which results in tons of csv files cluttering up the root of my drive. I got tired of deleting or backing up all of these files so I did what any developer would do and automated it!
Writeup courtesy of ChatGPT
Â
Are you tired of manually organizing CSV files in your Google Drive? Let’s explore a simple yet powerful Google Apps Script that automatically archives your CSV files every night, keeping your Drive tidy and organized.
Â
Â
Understanding the Script
This custom script is designed to streamline your Google Drive management by automatically archiving CSV files created within the last 24 hours. Here’s what it does.
- Scans your root Google Drive folder for recently created CSV files
- Creates a structured archive with year and month subfolders
- Moves CSV files to the appropriate subfolder
- Renames files to avoid conflicts
- Deletes the original file from your main Drive
Step-by-Step Setup Guide
Setting up this automation is straightforward. Follow these steps to get your nightly CSV archiver up and running.
Open Google Drive and create a new Google Apps Script
- Click on New > More > Google Apps Script
- Paste the script code there
View Code
Save your project
- Click on File > Save
- Give your project a name, like “Daily CSV Archiver”
Set up the nightly trigger
- Click on Edit > Current project’s triggers
- Click on + Add Trigger in the bottom right corner
- Configure the trigger as follows:
- Choose which function to run: archiveRecentCSVs
- Which runs at deployment: Head
- Select event source: Time-driven
- Select type of time based trigger: Day timer
- Select time of day: Midnight to 1am
Save the trigger and authorize the script
- Click Save
- Follow the prompts to authorize the script to access your Google Drive
That’s it! Your CSV archiver is now set up to run automatically every night between midnight and 1 AM.
Customization and Troubleshooting
While the script works great out of the box, you might want to customize it for your specific needs:
- To archive different file types, modify the MIME type check in the script
- Adjust the time range by changing the oneDayInMilliseconds variable
- Modify folder names by changing the strings in getFoldersByName() calls
If you encounter any issues
- Check that the script has the necessary permissions to access your Drive
- Ensure your Google Drive has the required archive folders set up
- Review the script logs (View > Logs) for any error messages
Â
Â