如何为Google Drive文件夹及子文件夹内的所有文件批量应用标签?
Great question—manually tagging files across nested folders is such a time sink, especially when your structure is alphabetically organized with small batches per subfolder. Here are two reliable methods to skip the repetitive work and apply tags to every file in a folder (and all its subfolders) in one go:
Method 1: Use Google Apps Script (Custom Automated Workflow)
This is the most flexible option, perfect for handling large folder trees without manual selection limits.
Step-by-Step Setup:
- Open Google Apps Script (or from Google Drive: New > More > Google Apps Script).
- In the script editor, first enable the Drive API:
- Click Services (left sidebar) > Add Service > Select Drive API (v3) > Click Add.
- Replace the default
myFunction()code with the script below:
function batchApplyTagToFolderTree() { // Replace these values with your own const targetFolderId = "YOUR_FOLDER_ID"; // Get from folder URL (e.g., https://drive.google.com/drive/folders/[THIS_PART]) const tagName = "YOUR_TARGET_TAG"; // Must match an existing tag in your Drive // Find the tag ID by name const allTags = Drive.Tags.list().tags; const targetTag = allTags.find(tag => tag.name === tagName); if (!targetTag) { console.log(`Error: Tag "${tagName}" not found. Double-check the tag name.`); return; } // Recursively process all files in folder and subfolders function processFolder(folder) { // Handle files in current folder const files = folder.getFiles(); while (files.hasNext()) { const file = files.next(); // Apply the tag to the file Drive.Files.update( { labelIds: [targetTag.id] }, file.getId(), null, { fields: 'labelIds' } ); console.log(`Tagged: ${file.getName()}`); } // Handle subfolders const subFolders = folder.getFolders(); while (subFolders.hasNext()) { processFolder(subFolders.next()); } } // Start the workflow const rootFolder = DriveApp.getFolderById(targetFolderId); processFolder(rootFolder); console.log("Batch tagging complete!"); }
- Replace
YOUR_FOLDER_IDandYOUR_TARGET_TAGwith your actual folder ID and tag name. - Click the run button (▶️) and authorize the script when prompted (you’ll need to allow access to your Drive files).
Notes:
- If you have tens of thousands of files, you may hit Google’s API quota limits. In that case, add a
Utilities.sleep(100)inside the file loop to slow down requests. - The script will log every tagged file in the Executions tab so you can verify progress.
Method 2: Advanced Search + Bulk Selection (No Code Needed)
If you prefer a no-code approach, use Google Drive’s advanced search to select all files in the folder tree at once:
- Open Google Drive and paste this into the search bar (replace
YOUR_FOLDER_IDwith your folder’s ID):
This search returns every file in the target folder and all its subfolders.parents:"YOUR_FOLDER_ID" - Select the first file, then press
Ctrl+A(Windows) orCmd+A(Mac) to select all visible files. - A prompt will appear at the top: "Select all X items that match this search"—click this to select every file in the results (even those not visible on the page).
- Right-click the selected files, hover over Tags, and check the tag you want to apply.
Notes:
- This method works best for folder trees with fewer than ~5,000 files (larger sets may take time to load or cause browser slowdowns).
- Make sure your tag is already created in Drive (via the left sidebar’s Tags menu) before starting.
内容的提问来源于stack exchange,提问作者Adam
相关产品推荐
相关产品推荐

