Creating multiple sub-folders in Google Drive automatically can save time, especially when you need a specific folder structure. Here's how you can do it using various methods:
Method 1: Using Google Apps Script
Google Apps Script is a free scripting platform integrated with Google Workspace. You can use it to create multiple folders programmatically.
-
Open Google Apps Script:
- Go to Google Apps Script and create a new project.
-
Add the Script: Paste the following code into the editor:
function createSubFolders() { var parentFolderId = "YOUR_PARENT_FOLDER_ID"; // Replace with your folder ID var folderNames = ["Folder1", "Folder2", "Folder3"]; // List of sub-folder names var parentFolder = DriveApp.getFolderById(parentFolderId); folderNames.forEach(function(folderName) { parentFolder.createFolder(folderName); }); Logger.log("Folders created successfully!"); }Replace
"YOUR_PARENT_FOLDER_ID"with the actual ID of the parent folder (found in the URL of the folder). -
Run the Script:
- Save and click the ▶️ Run button.
- Authorize the script if prompted.
- Check the parent folder to see the new sub-folders.
Method 2: Using a Spreadsheet with Google Apps Script
If you have a list of folder names in a Google Sheet, you can automate folder creation directly from the sheet.
-
Prepare a Google Sheet:
- In column A, list the folder names you want to create.
-
Add the Script: Paste this code in the Apps Script editor:
function createFoldersFromSheet() { var parentFolderId = "YOUR_PARENT_FOLDER_ID"; // Replace with your folder ID var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var folderNames = sheet.getRange(1, 1, sheet.getLastRow(), 1).getValues(); var parentFolder = DriveApp.getFolderById(parentFolderId); folderNames.forEach(function(name) { parentFolder.createFolder(name[0]); }); Logger.log("Folders created from sheet!"); } -
Run the Script:
- Save and execute the script.
- Check the parent folder for the newly created sub-folders.
Method 3: Using Third-Party Tools
Some tools and scripts, such as Zapier, Integromat, or custom Python scripts, can also automate folder creation in Google Drive. These may require API access.
Method 4: Using Python with Google Drive API
-
Enable Google Drive API:
- Go to the Google Cloud Console and enable the Google Drive API for your project.
-
Install Dependencies: Run:
pip install google-api-python-client google-auth google-auth-oauthlib google-auth-httplib2 -
Use the Script:
from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload from google.oauth2.service_account import Credentials SCOPES = ['https://www.googleapis.com/auth/drive'] SERVICE_ACCOUNT_FILE = 'path_to_your_service_account.json' creds = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES) service = build('drive', 'v3', credentials=creds) parent_folder_id = 'YOUR_PARENT_FOLDER_ID' folder_names = ['Folder1', 'Folder2', 'Folder3'] for name in folder_names: file_metadata = { 'name': name, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [parent_folder_id] } service.files().create(body=file_metadata, fields='id').execute() print("Folders created!")
Tips:
- Always double-check folder permissions to ensure the script can access and modify your Drive.
- Test your script with a small number of folders before scaling up.
Let me know if you'd like further assistance!
0 Comments