Skip to content

Commit 9134c60

Browse files
committed
Rename Files
1 parent 246d4d6 commit 9134c60

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
node_modules/
2-
dist
2+
dist
3+
.eslintrc.json
4+
babel.config.js
5+
package-lock.json
6+
package.json

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Google Apps Script is a scripting language based on JavaScript that lets you do
44

55
## About the developer
66

7-
These [Google Apps Script](https://www.labnol.org/topic/google-apps-script) code snippets and [Google Workspace add-ons] are coded by [Amit Agarwal](https://www.labnol.org/about) for the [Digital Inspiration](https://digitalinspiration.com/).
7+
These [Google Apps Script](https://www.labnol.org/topic/google-apps-script) code snippets and [Google Workspace add-ons] are coded by [Amit Agarwal](https://www.labnol.org/about) for [Digital Inspiration](https://digitalinspiration.com/).
88

99
## Google Apps Script Snippets
1010

@@ -20,6 +20,7 @@ These [Google Apps Script](https://www.labnol.org/topic/google-apps-script) code
2020
| [YouTube Alerts](./google-apps-script/youtube-alerts/) | Monitor YouTube videos with Google Sheets |
2121
| [YouTube Views](./google-apps-script/youtube-views/) | Get the views of any YouTube video |
2222
| [Zoom Scheduler](./google-apps-script/zoom-meetings/) | Schedule Zoom meetings with Google Sheets |
23+
| [Rename Files](./google-apps-script/rename-files/) | Rename files in Google Drive with AI |
2324

2425
## Google Workspace Add-ons
2526

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rename Files
2+
3+
[Rename Files in Google Drive with AI](https://www.labnol.org/rename-files-in-google-drive-240129)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"timeZone": "Asia/Kolkata",
3+
"dependencies": {
4+
"enabledAdvancedServices": [
5+
{
6+
"userSymbol": "Drive",
7+
"version": "v3",
8+
"serviceId": "drive"
9+
}
10+
]
11+
},
12+
"exceptionLogging": "STACKDRIVER",
13+
"runtimeVersion": "V8"
14+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Rename Files in Google Drive using Gemini AI
3+
*
4+
* Author: Amit Agarwal
5+
* Email: amit@labnol.org
6+
* Web: https://digitalinspiration.com/
7+
*
8+
* MIT License
9+
*/
10+
11+
const GoogleDriveFolderId = 'Your Drive Folder Id';
12+
const GoogleGeminiAPIKey = 'Your Gemini AI key';
13+
14+
const getFilesInFolder_ = (folderId) => {
15+
const mimeTypes = ['image/png', 'image/jpeg', 'image/webp'];
16+
const { files = [] } = Drive.Files.list({
17+
q: `'${folderId}' in parents and (${mimeTypes.map((type) => `mimeType='${type}'`).join(' or ')})`,
18+
fields: 'files(id,thumbnailLink,mimeType)',
19+
pageSize: 10,
20+
});
21+
return files;
22+
};
23+
24+
const getFileAsBase64_ = (thumbnailLink) => {
25+
const blob = UrlFetchApp.fetch(thumbnailLink).getBlob();
26+
const base64 = Utilities.base64Encode(blob.getBytes());
27+
return base64;
28+
};
29+
30+
const getSuggestedFilename_ = (base64, mimeType) => {
31+
const text = `Analyze the image content and propose a concise, descriptive filename in 5-15 words without providing any explanation or additional text. Use spaces instead of underscores. Append the extension based on file's mimeType which is ${mimeType}`;
32+
const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=${GoogleGeminiAPIKey}`;
33+
const inlineData = {
34+
mimeType,
35+
data: base64,
36+
};
37+
38+
try {
39+
const response = UrlFetchApp.fetch(apiUrl, {
40+
method: 'POST',
41+
headers: {
42+
'Content-Type': 'application/json',
43+
},
44+
payload: JSON.stringify({
45+
contents: [{ parts: [{ inlineData }, { text }] }],
46+
}),
47+
});
48+
const data = JSON.parse(response);
49+
return data.candidates[0].content.parts[0].text.trim();
50+
} catch (f) {
51+
return null;
52+
}
53+
};
54+
55+
const renameFilesInGoogleDrive = () => {
56+
const files = getFilesInFolder_(GoogleDriveFolderId);
57+
files.forEach((file) => {
58+
const { id, thumbnailLink, mimeType } = file;
59+
const base64 = getFileAsBase64_(thumbnailLink);
60+
const name = getSuggestedFilename_(base64, mimeType);
61+
if (name) {
62+
Drive.Files.update({ name }, id);
63+
}
64+
});
65+
};

0 commit comments

Comments
 (0)