index.js aktualisiert
This commit is contained in:
parent
1250b6c328
commit
160b56634c
333
index.js
333
index.js
@ -1,130 +1,203 @@
|
|||||||
const fs = require('fs');
|
/*
|
||||||
const path = require('path');
|
* Projekt: File Renamer CLI
|
||||||
const os = require('os');
|
* Beschreibung: Ein rekursives CLI-Tool zum Umbenennen von Dateien mit Suffix.
|
||||||
const readline = require('readline');
|
* Autor: M_Viper
|
||||||
|
* Lizenz: MIT
|
||||||
const configPath = path.join(os.homedir(), 'Documents', 'config.json');
|
* GitHub: https://git.viper.ipv64.net/M_Viper/file-renamer-cli
|
||||||
|
* Webseite: https://m-viper.de
|
||||||
function saveConfig(config) {
|
*/
|
||||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
|
|
||||||
}
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
function loadConfig() {
|
const os = require('os');
|
||||||
if (fs.existsSync(configPath)) {
|
const readline = require('readline');
|
||||||
try {
|
|
||||||
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
const configPath = path.join(os.homedir(), 'Documents', 'config.json');
|
||||||
} catch {
|
|
||||||
return null;
|
// ================== Banner ==================
|
||||||
}
|
function greenMessage(text) {
|
||||||
}
|
console.log('\x1b[32m%s\x1b[0m', text);
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
function centerText(text, width) {
|
||||||
function askQuestion(query) {
|
const len = text.length;
|
||||||
const rl = readline.createInterface({
|
if (len >= width) return text;
|
||||||
input: process.stdin,
|
const leftPadding = Math.floor((width - len) / 2);
|
||||||
output: process.stdout,
|
return ' '.repeat(leftPadding) + text;
|
||||||
});
|
}
|
||||||
return new Promise(resolve => rl.question(query, ans => {
|
|
||||||
rl.close();
|
function showAsciiLogo() {
|
||||||
resolve(ans.trim());
|
const width = process.stdout.columns || 80; // Terminalbreite, fallback 80
|
||||||
}));
|
|
||||||
}
|
const logoLines = [
|
||||||
|
'███████╗██╗██╗ ███████╗███╗ ██╗ █████╗ ███╗ ███╗███████╗ ██████╗██╗ ██╗',
|
||||||
async function getConfig() {
|
'██╔════╝██║██║ ██╔════╝████╗ ██║██╔══██╗████╗ ████║██╔════╝ ██╔════╝██║ ██║',
|
||||||
const existingConfig = loadConfig();
|
'█████╗ ██║██║ █████╗ ██╔██╗ ██║███████║██╔████╔██║█████╗ ██║ ██║ ██║',
|
||||||
if (existingConfig?.folderPath && existingConfig?.suffix) {
|
'██╔══╝ ██║██║ ██╔══╝ ██║╚██╗██║██╔══██║██║╚██╔╝██║██╔══╝ ██║ ██║ ██║',
|
||||||
console.log(`Verwende gespeicherte Konfiguration:`);
|
'██║ ██║███████╗███████╗██║ ╚████║██║ ██║██║ ╚═╝ ██║███████╗ ╚██████╗███████╗██║',
|
||||||
console.log(`Ordner: ${existingConfig.folderPath}`);
|
'╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═════╝╚══════╝╚═╝',
|
||||||
console.log(`Suffix: ${existingConfig.suffix}`);
|
];
|
||||||
return existingConfig;
|
|
||||||
}
|
console.log('\x1b[32m'); // grün starten
|
||||||
|
for (const line of logoLines) {
|
||||||
console.log('Möchtest du den automatischen Ordner nutzen?');
|
console.log(centerText(line, width));
|
||||||
console.log('1 = Ja (Desktop\\Filme)');
|
}
|
||||||
console.log('2 = Benutzerdefinierter Ordner');
|
console.log('\x1b[0m'); // Farbe zurücksetzen
|
||||||
|
console.log('');
|
||||||
const antwort = await askQuestion('Deine Wahl (1 oder 2): ');
|
}
|
||||||
|
|
||||||
let folderPath;
|
function showBanner() {
|
||||||
|
console.clear();
|
||||||
if (antwort === '1') {
|
showAsciiLogo();
|
||||||
folderPath = path.join(os.homedir(), 'Desktop', 'Filme');
|
|
||||||
console.log(`Automatischer Ordner gewählt: ${folderPath}`);
|
const width = process.stdout.columns || 60; // Breite des Terminals, fallback 60
|
||||||
} else if (antwort === '2') {
|
|
||||||
folderPath = await askQuestion('Bitte gib den vollständigen Pfad zum Ordner ein:\n');
|
const bannerLines = [
|
||||||
} else {
|
'Version 1.0',
|
||||||
console.log('Ungültige Eingabe, bitte versuche es nochmal.');
|
'Script by',
|
||||||
return getConfig();
|
'@M_Viper',
|
||||||
}
|
'__________________________',
|
||||||
|
'',
|
||||||
const suffix = await askQuestion('Welcher Text soll am Ende der Dateinamen hinzugefügt werden? (z. B. @Name)\n');
|
'Git: https://git.viper.ipv64.net/M_Viper/file-renamer-cli',
|
||||||
|
];
|
||||||
const config = { folderPath, suffix };
|
|
||||||
saveConfig(config);
|
// Obere Rahmenlinie
|
||||||
return config;
|
greenMessage('╔' + '═'.repeat(width - 2) + '╗');
|
||||||
}
|
|
||||||
|
// Bannerzeilen mit Rahmen und Zentrierung
|
||||||
function umbenennenSync(pfad, suffix) {
|
for (const line of bannerLines) {
|
||||||
console.log('Prüfe Ordner:', pfad);
|
const centered = centerText(line, width - 4); // 4 wegen Rahmen links und rechts + Leerzeichen
|
||||||
|
greenMessage('║ ' + centered + ' ║');
|
||||||
if (!fs.existsSync(pfad)) {
|
}
|
||||||
console.log('Ordner existiert nicht, erstelle:', pfad);
|
|
||||||
fs.mkdirSync(pfad, { recursive: true });
|
// Untere Rahmenlinie
|
||||||
return;
|
greenMessage('╚' + '═'.repeat(width - 2) + '╝');
|
||||||
}
|
greenMessage('');
|
||||||
|
}
|
||||||
const eintraege = fs.readdirSync(pfad, { withFileTypes: true });
|
// ============================================
|
||||||
|
|
||||||
if (eintraege.length === 0) {
|
function saveConfig(config) {
|
||||||
console.log('Ordner ist leer:', pfad);
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const eintrag of eintraege) {
|
function loadConfig() {
|
||||||
const vollerPfad = path.join(pfad, eintrag.name);
|
if (fs.existsSync(configPath)) {
|
||||||
|
try {
|
||||||
if (eintrag.isDirectory()) {
|
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
||||||
if (
|
} catch {
|
||||||
eintrag.name === 'System Volume Information' ||
|
return null;
|
||||||
eintrag.name.startsWith('$') ||
|
}
|
||||||
eintrag.name.startsWith('.')
|
}
|
||||||
) {
|
return null;
|
||||||
console.log('Überspringe geschützten/verborgenen Ordner:', vollerPfad);
|
}
|
||||||
continue;
|
|
||||||
}
|
function askQuestion(query) {
|
||||||
console.log('Betrete Unterordner:', vollerPfad);
|
const rl = readline.createInterface({
|
||||||
umbenennenSync(vollerPfad, suffix);
|
input: process.stdin,
|
||||||
} else if (eintrag.isFile()) {
|
output: process.stdout,
|
||||||
const ext = path.extname(eintrag.name);
|
});
|
||||||
const name = path.basename(eintrag.name, ext);
|
return new Promise(resolve => rl.question(query, ans => {
|
||||||
|
rl.close();
|
||||||
if (name.endsWith(` ${suffix}`)) {
|
resolve(ans.trim());
|
||||||
console.log('Datei schon umbenannt, überspringe:', eintrag.name);
|
}));
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
async function getConfig() {
|
||||||
const neuerName = `${name} ${suffix}${ext}`;
|
const existingConfig = loadConfig();
|
||||||
const neuerPfad = path.join(pfad, neuerName);
|
if (existingConfig?.folderPath && existingConfig?.suffix) {
|
||||||
|
console.log(`Verwende gespeicherte Konfiguration:`);
|
||||||
try {
|
console.log(`Ordner: ${existingConfig.folderPath}`);
|
||||||
fs.renameSync(vollerPfad, neuerPfad);
|
console.log(`Suffix: ${existingConfig.suffix}`);
|
||||||
console.log(`Umbenannt: ${eintrag.name} → ${neuerName}`);
|
return existingConfig;
|
||||||
} catch (e) {
|
}
|
||||||
console.error(`Fehler bei ${eintrag.name}:`, e.message);
|
|
||||||
if (e.code === 'EPERM' || e.code === 'EACCES') {
|
console.log('Möchtest du den automatischen Ordner nutzen?');
|
||||||
console.error('Keine Zugriffsrechte oder Datei geschützt. Übersprungen.');
|
console.log('1 = Ja (Desktop\\Filme)');
|
||||||
}
|
console.log('2 = Benutzerdefinierter Ordner');
|
||||||
}
|
|
||||||
} else {
|
const antwort = await askQuestion('Deine Wahl (1 oder 2): ');
|
||||||
console.log('Kein Datei- oder Ordner-Eintrag, überspringe:', eintrag.name);
|
|
||||||
}
|
let folderPath;
|
||||||
}
|
|
||||||
}
|
if (antwort === '1') {
|
||||||
|
folderPath = path.join(os.homedir(), 'Desktop', 'Filme');
|
||||||
async function main() {
|
console.log(`Automatischer Ordner gewählt: ${folderPath}`);
|
||||||
const config = await getConfig();
|
} else if (antwort === '2') {
|
||||||
umbenennenSync(config.folderPath, config.suffix);
|
folderPath = await askQuestion('Bitte gib den vollständigen Pfad zum Ordner ein:\n');
|
||||||
console.log('Fertig!');
|
} else {
|
||||||
}
|
console.log('Ungültige Eingabe, bitte versuche es nochmal.');
|
||||||
|
return getConfig();
|
||||||
main();
|
}
|
||||||
|
|
||||||
|
const suffix = await askQuestion('Welcher Text soll am Ende der Dateinamen hinzugefügt werden? (z. B. @Name)\n');
|
||||||
|
|
||||||
|
const config = { folderPath, suffix };
|
||||||
|
saveConfig(config);
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
function umbenennenSync(pfad, suffix) {
|
||||||
|
console.log('Prüfe Ordner:', pfad);
|
||||||
|
|
||||||
|
if (!fs.existsSync(pfad)) {
|
||||||
|
console.log('Ordner existiert nicht, erstelle:', pfad);
|
||||||
|
fs.mkdirSync(pfad, { recursive: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const eintraege = fs.readdirSync(pfad, { withFileTypes: true });
|
||||||
|
|
||||||
|
if (eintraege.length === 0) {
|
||||||
|
console.log('Ordner ist leer:', pfad);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const eintrag of eintraege) {
|
||||||
|
const vollerPfad = path.join(pfad, eintrag.name);
|
||||||
|
|
||||||
|
if (eintrag.isDirectory()) {
|
||||||
|
if (
|
||||||
|
eintrag.name === 'System Volume Information' ||
|
||||||
|
eintrag.name.startsWith('$') ||
|
||||||
|
eintrag.name.startsWith('.')
|
||||||
|
) {
|
||||||
|
console.log('Überspringe geschützten/verborgenen Ordner:', vollerPfad);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
console.log('Betrete Unterordner:', vollerPfad);
|
||||||
|
umbenennenSync(vollerPfad, suffix);
|
||||||
|
} else if (eintrag.isFile()) {
|
||||||
|
const ext = path.extname(eintrag.name);
|
||||||
|
const name = path.basename(eintrag.name, ext);
|
||||||
|
|
||||||
|
if (name.endsWith(` ${suffix}`)) {
|
||||||
|
console.log('Datei schon umbenannt, überspringe:', eintrag.name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const neuerName = `${name} ${suffix}${ext}`;
|
||||||
|
const neuerPfad = path.join(pfad, neuerName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.renameSync(vollerPfad, neuerPfad);
|
||||||
|
console.log(`Umbenannt: ${eintrag.name} → ${neuerName}`);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Fehler bei ${eintrag.name}:`, e.message);
|
||||||
|
if (e.code === 'EPERM' || e.code === 'EACCES') {
|
||||||
|
console.error('Keine Zugriffsrechte oder Datei geschützt. Übersprungen.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Kein Datei- oder Ordner-Eintrag, überspringe:', eintrag.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
showBanner(); // Banner anzeigen
|
||||||
|
|
||||||
|
const config = await getConfig();
|
||||||
|
umbenennenSync(config.folderPath, config.suffix);
|
||||||
|
console.log('Fertig!');
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user