🤖 Automate Your Google Drive: Nightly CSV Archiving Made Easy

Tags
productivity
Published
Jul 14, 2024
notion image
Inspiration
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

  1. Click on New > More > Google Apps Script
  1. Paste the script code there
page icon
View Code

Save your project

  1. Click on File > Save
  1. Give your project a name, like “Daily CSV Archiver”

Set up the nightly trigger

  1. Click on Edit > Current project’s triggers
  1. Click on + Add Trigger in the bottom right corner
  1. 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
Trigger Configuration
Trigger Configuration

Save the trigger and authorize the script

  1. Click Save
  1. 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
 
notion image
Â