Still struggling with listing files from Google Drive manually? I’ve been there too.

Still struggling with listing files from Google Drive manually? I’ve been there too.


I used to spend hours copying file names and URLs one by one. It was tiring, and let’s be honest, no one wants to do that! 😩

After some work (and more coffee than I’d like to admit ☕), I finally wrote a simple script to do it all for me. Now, I’m sharing it with you so you can save your time, too.

𝗛𝗼𝘄 𝗱𝗼𝗲𝘀 𝗶𝘁 𝗵𝗲𝗹𝗽?

– It lists all the files from a folder and puts their shareable URLs into a Google Sheet.
– Before using it, make sure your folder’s access is set to “Anyone with the link” so the script can create those shareable URLs.
– Due to time limits (around 6 minutes per run), I’ve set it to process 100 files at a time. After it finishes, just run it again if you have more files. Repeat as many times as needed until you have all your URLs.

𝗛𝗲𝗿𝗲’𝘀 𝘁𝗵𝗲 𝗰𝗼𝗱𝗲:

function listFilesInFolder() {
var folderId = 'YOUR_ID_FOLDER';
var maxFilesPerRun = 100;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

var props = PropertiesService.getScriptProperties();
var startIndex = parseInt(props.getProperty('lastProcessedIndex')) || 0;

if (startIndex === 0) {
sheet.clear();
sheet.appendRow(["File name", "URL for sharing"]);
}

var folder = DriveApp.getFolderById(folderId);
var files = folder.getFiles();

var currentIndex = 0;
var processedCount = 0;

while (files.hasNext()) {
var file = files.next();

if (currentIndex < startIndex) {
currentIndex++;
continue;
}

var fileName = file.getName();
var fileUrl = file.getUrl();

if (file.getSharingAccess() === DriveApp.Access.PRIVATE) {
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
}

sheet.appendRow([fileName, fileUrl]);
processedCount++;
currentIndex++;

if (processedCount >= maxFilesPerRun) {
props.setProperty('lastProcessedIndex', currentIndex.toString());
Logger.log("Stop after " + processedCount + " files. The next launch will start with the index " + currentIndex);
return;
}
}

props.deleteProperty('lastProcessedIndex');
SpreadsheetApp.getUi().alert("It's done! The links are saved in the table.");
}


I’ll also share a video example, so you can see it in action.



This script is part of a bigger project I mentioned in previous posts, and it’s just one of the many tools I use to save time.

Hope it helps you too! Feel free to ask any questions.

Let’s make technical SEO a bit more fun and a lot more efficient!

Was this article helpful?
YesNo
Leave a Reply 0

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


× Contact me