Hey everyone! I’m trying to sort out a bunch of files in my Google Drive. They’re all in one folder right now, but I want to organize them better.
Here’s what I’m dealing with:
- I’ve got a mix of PSD and JPG files
- The file names start with a number, like 082234567
- I want to create folders using that starting number
- Then move all files with the same starting number into their matching folder
For instance, if I have files like:
123456789_design.psd
123456789_image1.jpg
123456789_image2.jpg
987654321_design.psd
987654321_image1.jpg
I’d like to end up with two folders: ‘123456789’ and ‘987654321’, each containing their respective files.
Can anyone help me write an Apps Script to do this automatically? I’m not great with coding, so any tips or examples would be super helpful. Thanks in advance!
I’ve tackled a similar organization challenge with Google Drive, and I can share what worked for me. First, you’ll need to create a function that extracts the number prefix from each filename. Then, use that to create folders and move files accordingly. Here’s a basic structure to get you started:
function organizeFiles() {
var folder = DriveApp.getFolderById('YOUR_FOLDER_ID');
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var fileName = file.getName();
var prefix = fileName.substring(0, 9);
var subFolder = folder.createFolder(prefix);
file.moveTo(subFolder);
}
}
Replace ‘YOUR_FOLDER_ID’ with the ID of your source folder. This script creates a new folder for each unique prefix and moves the corresponding files into it. You might need to adjust the substring length if your number prefixes vary. Remember to test this on a small batch of files first to ensure it works as expected.
As someone who’s wrestled with organizing massive amounts of files in Google Drive, I can totally relate to your predicament. I actually developed a script for a similar task at my previous job. Here’s a slightly more robust version that might work for you:
function organizeFiles() {
var sourceFolder = DriveApp.getFolderById(‘YOUR_SOURCE_FOLDER_ID’);
var files = sourceFolder.getFiles();
var folderMap = {};
while (files.hasNext()) {
var file = files.next();
var fileName = file.getName();
var prefix = fileName.substr(0, 9);
if (!folderMap[prefix]) {
folderMap[prefix] = sourceFolder.createFolder(prefix);
}
file.moveTo(folderMap[prefix]);
}
}
This script creates folders only when needed and reuses existing ones, which can save time if you’re dealing with lots of files. Just remember to replace ‘YOUR_SOURCE_FOLDER_ID’ with the actual ID of your folder. Hope this helps!
hey, ive done something similar before. heres a quick script that might help:
function sortFiles() {
var folder = DriveApp.getFolderById(‘your_folder_id’);
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var prefix = file.getName().substring(0,9);
var subFolder = folder.createFolder(prefix);
file.moveTo(subFolder);
}
}
just replace ‘your_folder_id’ with ur actual folder ID. good luck!