Created
April 3, 2023 21:41
-
-
Save philsinatra/509aa34ca5f4a52f7a6109f37d6062f8 to your computer and use it in GitHub Desktop.
Organize images into subfolders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const MAX_FILES_PER_FOLDER = 500; | |
const IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif']; | |
// Get a list of all files in the current directory | |
const files = fs.readdirSync('.'); | |
// Filter out only the image files | |
const image_files = files.filter(file => { | |
const extension = path.extname(file).toLowerCase(); | |
return IMAGE_EXTENSIONS.includes(extension); | |
}); | |
// Determine how many subfolders we need | |
const num_folders = Math.ceil(image_files.length / MAX_FILES_PER_FOLDER); | |
console.log(`Found ${image_files.length} image files.`); | |
console.log(`Creating ${num_folders} folders with a maximum of ${MAX_FILES_PER_FOLDER} files per folder.`); | |
// Create the subfolders and move the files into them | |
for (let i = 0; i < num_folders; i++) { | |
const folder_name = `Folder ${i + 1}`; | |
fs.mkdirSync(folder_name); | |
const start_index = i * MAX_FILES_PER_FOLDER; | |
const end_index = Math.min((i + 1) * MAX_FILES_PER_FOLDER, image_files.length); | |
const files_to_move = image_files.slice(start_index, end_index); | |
console.log(`Moving ${files_to_move.length} files to ${folder_name}.`); | |
files_to_move.forEach(file => { | |
const old_path = path.join('.', file); | |
const extension = path.extname(file).toLowerCase(); | |
let new_path = path.join(folder_name, file); | |
if (extension !== path.extname(new_path).toLowerCase()) { | |
new_path += extension; | |
} | |
fs.renameSync(old_path, new_path); | |
}); | |
} | |
console.log('Done.'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const ORIGINAL_FOLDER_NAME = 'Original Files'; | |
// Get a list of all subfolders | |
const subfolders = fs.readdirSync('.') | |
.filter(file => fs.lstatSync(file).isDirectory()); | |
// Find the folder that contains the original files | |
const original_folder = subfolders.find(folder => folder.toLowerCase() === ORIGINAL_FOLDER_NAME.toLowerCase()); | |
if (!original_folder) { | |
console.error(`Could not find folder "${ORIGINAL_FOLDER_NAME}".`); | |
process.exit(1); | |
} | |
// Get a list of all files in the subfolders | |
const subfolder_files = subfolders.map(folder => { | |
return { | |
folder_name: folder, | |
files: fs.readdirSync(folder) | |
}; | |
}); | |
// Create the original folder if it doesn't already exist | |
if (!fs.existsSync(ORIGINAL_FOLDER_NAME)) { | |
fs.mkdirSync(ORIGINAL_FOLDER_NAME); | |
} | |
// Move all files back into the original folder | |
subfolder_files.forEach(({ folder_name, files }) => { | |
console.log(`Moving ${files.length} files from ${folder_name} to ${ORIGINAL_FOLDER_NAME}.`); | |
files.forEach(file => { | |
const old_path = path.join(folder_name, file); | |
const new_path = path.join(ORIGINAL_FOLDER_NAME, file); | |
fs.renameSync(old_path, new_path); | |
}); | |
}); | |
// Delete all empty subfolders | |
subfolders.forEach(folder => { | |
if (fs.readdirSync(folder).length === 0) { | |
console.log(`Deleting empty folder: ${folder}`); | |
fs.rmdirSync(folder); | |
} | |
}); | |
console.log('Done.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment