Select Page

Using Google Apps Script To Unzip .Zip Files – Automate Folder Creation in Google Drive

by | 3 July 2024

In today’s fast-paced business world, efficiency is key. As a business owner and consultant, I’ve learned that the difference between success and stagnation often lies in the small details – like how we manage our digital workspace. One of the most time-consuming tasks I’ve encountered is manually creating folder structures in Google Drive, especially when dealing with repetitive projects or content creation processes.

But what if I told you there’s a way to automate this tedious task? A method that not only saves time but also ensures consistency across your projects? Enter Google Apps Script – a powerful tool that can transform the way you interact with Google Drive.

In this article, I’ll share a script I’ve developed that automatically unzips, .zip files and creates folder structures in Google Drive. Whether you’re a business professional looking to streamline your workflow or a curious mind interested in the possibilities of automation, this guide will walk you through the process, explain the code, and showcase real-world applications.

Why Automate Folder Creation?

Before we dive into the technical details, let’s consider why this automation is so valuable:

  1. Time Savings: Creating folders manually can take anywhere from 5 to 10 minutes per project. This might not seem like much, but it adds up quickly when you’re handling multiple projects daily.
  2. Consistency: With automation, you ensure that every project follows the same folder structure, making it easier for team members to find and organize files.
  3. Reduced Human Error: Automation eliminates the risk of forgetting to create essential folders or mistyping folder names.
  4. Scalability: As your business grows, this automation scales with you, handling increased workload without additional time investment.
  5. Integration Potential: The script can be integrated with other tools like Make.com (formerly Integromat) to create even more powerful workflows.

Understanding the Script

Now, let’s break down the script and understand its components. Don’t worry if you’re not a developer – I’ll explain each part in simple terms.

The Main Function

The heart of our script is the extractZipAndCreateFolders function. Here’s what it does:

  1. It takes three parameters:
    • fileId: The ID of the zip file in Google Drive
    • destinationFolderId: The ID of the folder where we want to create our new structure
    • newFolderName: (Optional) A custom name for the main folder
  2. It unzips the file and creates a folder structure based on the contents of the zip file.
  3. It handles errors gracefully, ensuring that if one file fails, the rest of the process continues.
  4. It returns a JSON string with detailed information about the created folders and files.

Helper Functions

The script includes several helper functions that make the main function work:

  1. createFolderStructure: Creates the necessary folders based on the file paths in the zip.
  2. getFolderIdFromPath: Retrieves the ID of a folder given its path.
  3. getUniqueFolderName: Ensures that folder names are unique by appending a timestamp if necessary.

The script also includes several security measures to ensure safe usage when deployed as a web app:

  • API Key Authentication: Ensures only authorized requests are processed.
  • Rate Limiting: Prevents abuse by limiting how frequently the script can be called.
  • Input Validation: Checks that the provided file IDs are in the correct format.
  • File Access Verification: Confirms that the script has access to the specified files.

Setting Up Your API Key

At the top of the script, you’ll find a CONFIG object. Replace ‘YOUR_SECRET_API_KEY’ with your actual API key:

// Configuration const CONFIG = {
API_KEY: 'YOUR_SECRET_API_KEY' // Replace with your actual API key 
};

Full Google Apps Script To UnZip .Zip Files

Publishing Your Script for External Use

To use this script with external tools like Make.com, you need to publish it as a web app. Here’s how to do it:

  1. Open your Google Apps Script project.
  2. Click on the “Deploy” button at the top right of the script editor.
  3. Select “New deployment” from the dropdown menu.
  4. In the “New deployment” window:
    • Click the gear icon next to “Select type” and choose “Web app”.
    • Under “Execute as”, select your account.
    • Under “Who has access”, choose “Anyone” if you want it to be publicly accessible, or “Anyone with Google Account” for more restricted access.
    • Click “Deploy”.
  5. Google will ask you to authorize the app. Review the permissions and click “Authorize”.
  6. You’ll receive a URL for your web app. Save this URL – you’ll need it to call your script from external tools.

Using the Script with Make.com

Now that your script is published as a web app, you can call it from Make.com (or any other tool that can make HTTP requests). Here’s how to set it up in Make.com:

  1. In your Make.com scenario, add a “HTTP” module.
  2. Set the method to “POST”.
  3. In the “URL” field, paste the web app URL you received when publishing your script.
  4. In the “Body” section, select “Raw” and set the content type to “JSON (application/json)”.
  5. In the body, include your parameters like this:
{
  "apiKey": "YOUR_SECRET_API_KEY",
  "fileId": "your_zip_file_id_here",
  "destinationFolderId": "your_destination_folder_id_here",
  "newFolderName": "Your New Folder Name"
}

Important: In the Make HTTP module settings, click on “Show advanced settings”. Set both “Follow redirect” and “Follow all redirects” to “Yes”.

Picture of Make.com's Advanced HTTP module setting

This last step is crucial for handling Google’s security redirects when accessing Apps Script web apps.

In the subsequent modules of your Make.com scenario, you can parse the JSON response from the script and use the information as needed.

Remember to keep your API key secure and not share it publicly. Also, update your script’s authorization settings in Google Apps Scripts if you make changes to the code after publishing. You may need to create a new deployment for significant changes.

By following these steps, you’ve now made your Google Apps Script accessible to external tools, greatly expanding its potential for automation and integration in your workflows.

Troubleshooting

If you encounter issues when calling the script from Make.com, here are some things to check:

  1. Ensure the API key in your Make.com request matches the one in your script.
  2. Double-check that you’ve enabled both “Follow redirect” and “Follow all redirects” in the HTTP module settings.
  3. Verify that your Google Apps Script is properly deployed as a web app and that you’re using the correct URL.
  4. Check the execution logs in your Google Apps Script project for any error messages.

Real-World Applications

Let’s look at some practical ways this script can be used:

1. Content Creation Workflow

As someone who manages content creation, I use this script to set up project folders for each new piece of content. Here’s how it works:

  1. I have a template zip file with folders for research, drafts, media, and final output.
  2. When starting a new project, I run the script, which creates a new folder structure based on this template.
  3. The script returns the folder IDs, which I can then use to direct my team (or AI assistants) to the correct locations for their work.

This process ensures consistency across all our content projects and saves valuable time in project setup.

2. Production Run Management

For my e-commerce business, Oh Crap (Australia’s #1 non-plastic compostable dog poop bag), I use this script to set up folders for each production run:

  1. The zip template includes folders for order info, invoices, artwork, and logistics.
  2. Running the script creates this structure instantly, ready for all related documents.
  3. The returned folder information can be immediately added to our project management system, allowing team members to start work without delay.

3. Client Project Setup

As a consultant, I use this script to quickly set up project spaces for new clients:

  1. The zip template includes standard folders like contracts, communications, deliverables, and invoices.
  2. Running the script creates a consistent structure for each new client.
  3. This ensures that all client information is organized in the same way, making it easier to manage multiple clients efficiently.

Setting Up and Running the Script

To use this script, you’ll need to follow these steps:

  1. Open Google Apps Script by going to script.google.com.
  2. Create a new project and paste the full script into the editor.
  3. Save the project and give it a name.

To test the script, you can use the testExtractZipAndCreateFolders function:

** Remember to replace the fileId and destinationFolderId with your own Google Drive file IDs.

Add this to the bottom of your script in the Google Apps Script coding tool, and run the function with the ID’s you want to test it with.

Integration with Other Tools

This script becomes even more powerful when integrated with automation tools like Make.com. Here’s a basic example of how you might use it:

  1. Set up a new scenario in Make.com.
  2. Use a trigger module (e.g., “Watch Folder” in Google Drive).
  3. When a new zip file is detected, call the Google Apps Script using the “Google Apps Script” module.
  4. Use the returned JSON data to update your project management tool or notify team members.

or

Call this script from a make.com scenario (automation) to create all your content folders and add the relevant links back to your content planning sheet and project management system. Then let your AI agents add their work to the folders you have created.

Conclusion

Automating folder creation in Google Drive might seem like a small thing, but it’s these small efficiencies that add up to significant time savings and improved workflows. This script is just one example of how a little bit of code can make a big difference in your daily operations.

As businesses continue to digitize and automate, tools like this will become increasingly valuable. Whether you’re managing content creation, e-commerce operations, or client projects, finding ways to streamline repetitive tasks is crucial for scaling your business efficiently.

I encourage you to try this script in your own workflows. Experiment with different folder structures, integrate it with your existing tools, and see how it can improve your productivity.

What’s Next?

This script is just the beginning. There are countless other ways to automate and optimize your Google Drive workflows. In future posts, we’ll explore more advanced techniques, like automatically creating and populating Google Docs or Sheets, or integrating with other Google Workspace apps.

What automation challenges are you facing in your business? How could tools like this help you work more efficiently? I’d love to hear your thoughts and experiences. Leave a comment below or reach out on social media to continue the conversation.

Remember, in the world of business automation, “Marketing is a commodity, the process is priceless.” Let’s keep refining our processes and unlocking new levels of productivity together.

<a href="https://henryreith.co/author/henry/" target="_self">Henry Reith</a>

Henry Reith

CEO at Oh Crap. Creating & implementing processes to market businesses in less than 15 mins a day. 'marketing is a commodity, process is priceless'

Related Posts

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *