Automating Google Drive with Apps Script: Check or Create Files in Folders

In today’s digital environment, managing files efficiently can save a lot of time and hassle. Google Apps Script provides a powerful way to automate tasks within Google’s suite of services, including Google Drive. In this blog post, I’ll guide you through creating a simple script that checks if a specific file exists within a Google Drive folder and creates it if it doesn’t exist. If the file does exist, the script will simply return the file’s ID.

Getting Started with Google Apps Script

Google Apps Script is a cloud-based scripting language for light-weight application development in the G Suite platform. It provides easy ways to automate tasks across Google products and third party services.

Step 1: Open Google Apps Script

First, navigate to Google Apps Script and click on “New Project” to create a new script.

Step 2: Writing the Script

Once you’re in the script editor, paste the following code. This script uses the DriveApp service to interact with Google Drive:

function checkOrCreateFile(folderId, fileName, mimeType) {
// Get the folder by ID
var folder = DriveApp.getFolderById(folderId);
// Search for files with the specified name in the folder
var files = folder.getFilesByName(fileName);

// Check if the file exists
if (files.hasNext()) {
// File exists, return the file ID
var file = files.next();
Logger.log("File exists: " + file.getName() + ", ID: " + file.getId());
return file.getId();
} else {
// File does not exist, create a new file
var file = folder.createFile(fileName, '', mimeType);
Logger.log("File created: " + file.getName() + ", ID: " + file.getId());
return file.getId();
}
}

Step 3: Customize the Script

Replace folderId, fileName, and mimeType in the script with the appropriate values:

  • folderId: The ID of the Google Drive folder where you want to check or create the file.
  • fileName: The name of the file you’re looking for or want to create.
  • mimeType: The MIME type of the file, such as ‘text/plain’ for plain text files.

Step 4: Run the Script

Save your script and run the checkOrCreateFile function by selecting it from the function dropdown menu next to the play button. You will need to authorize the script to interact with your Google Drive the first time you run it.

Step 5: Check the Logs

After running the script, check the output by clicking on Executions or navigating to View > Logs. The logs will indicate whether the file was found or created, along with its ID.

Conclusion

This simple script demonstrates the basics of file management with Google Apps Script and Google Drive. It can be particularly useful for scenarios where file existence checks are necessary before proceeding with further actions, such as data logging or document updates.

By integrating such scripts into your workflow, you can significantly automate routine tasks, reduce errors, and increase efficiency. Google Apps Script is versatile and can be expanded further to accommodate more complex scenarios and integrate seamlessly across the entire Google Workspace.

To create a Google Apps Script that checks if a file exists in a specific folder on Google Drive and either creates the file if it does not exist or returns the ID of the existing file, you can use the Google Apps Script environment, which provides APIs to interact with Google Drive. Here’s a step-by-step script that accomplishes this task:

  1. Open the Google Apps Script Environment:
  2. Paste the following code into the script editor:
    • This script uses Google Drive’s DriveApp service to search for the file by name in a specified folder. If the file does not exist, it creates the file. If it does, it returns the file’s ID.
function checkOrCreateFile(folderId, fileName, mimeType) {
// Get the folder by ID
var folder = DriveApp.getFolderById(folderId);
// Search for files with the specified name in the folder
var files = folder.getFilesByName(fileName);

// Check if the file exists
if (files.hasNext()) {
// File exists, return the file ID
var file = files.next();
Logger.log("File exists: " + file.getName() + ", ID: " + file.getId());
return file.getId();
} else {
// File does not exist, create a new file
var file = folder.createFile(fileName, '', mimeType);
Logger.log("File created: " + file.getName() + ", ID: " + file.getId());
return file.getId();
}
}
  1. Modify the script parameters:
    • Replace folderId with the actual ID of the folder where you want to check or create the file.
    • Replace fileName with the name of the file you want to check or create.
    • Replace mimeType with the MIME type of the file. For a plain text file, use 'text/plain'.
  2. Run the script:
    • Save the script and run checkOrCreateFile by selecting the function in the dropdown menu near the run button.
    • The first time you run the script, you will need to authorize the script to access your Google Drive.
  3. View logs for output:
    • View the logs by clicking on Executions or View > Logs to see the output, which will show whether the file was found or created, along with the file ID.

This script provides a basic implementation of checking for a file in a folder and creating it if it doesn’t exist, leveraging Google Apps Script’s powerful integration with Google Drive. You can expand this script with additional features like checking the file type, managing different file formats, or handling more complex scenarios based on your needs.