-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (124 loc) Β· 5.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
const { log } = require('node:console');
const fs = require('node:fs');
const paths = require('path');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const dirdata = {};
let filedata = [];
// Prompt the user for the path
rl.question('π Enter the path to organize: ', (inputPath) => {
const path = inputPath.trim(); // Store path here for later use
// Read the directory
fs.readdir(path, (error, files) => {
if (error) {
console.log('β Error:', error.message);
rl.close();
return;
}
filedata = files.filter(file => {
// Skip folders and files without extensions
return fs.lstatSync(paths.join(path, file)).isFile() && file.includes('.');
});
if (filedata.length === 0) {
console.log('β οΈ No files to organize! Exiting...');
rl.close();
return;
}
filedata.map((file) => {
const splitfile = file.split('.');
// Process only files with an extension
if (splitfile.length > 1) {
const ext = splitfile[1];
const filename = splitfile[0];
const doesExtensionExist = Object.keys(dirdata).find((extension) => extension === ext);
if (doesExtensionExist) {
dirdata[ext] = [...dirdata[ext], filename];
} else {
dirdata[ext] = [filename];
}
}
});
// Ask if the user wants to customize folder names
rl.question('π§ Do you want to customize folder names for file extensions? (yes to customize, press Enter to use defaults): ', (customizeChoice) => {
if (customizeChoice.toLowerCase() === 'yes') {
console.log('π¨ Customization mode: You can change folder names for each file extension.');
// Proceed with customization
processExtensionsWithCustomization(path);
} else {
console.log('β
Using default folder names for extensions...');
// Proceed with default folder names
processExtensionsWithDefaults(path);
}
});
});
});
// Function to process extensions with default folder names
const processExtensionsWithDefaults = (path) => {
const extensions = Object.entries(dirdata);
const processExtensions = (index) => {
if (index >= extensions.length) {
console.log('β
All files have been organized successfully with default folder names! Closing...');
rl.close();
return;
}
const [ext, files] = extensions[index];
const folderName = `${ext}-files`; // Default folder name
fs.mkdir(paths.join(path, folderName), { recursive: true }, (error) => {
if (error) {
console.log('β Error creating folder:', error.message);
} else {
console.log(`π Folder "<${folderName}>" created!`);
}
files.map((file_ext) => {
const targetPath = paths.join(path, folderName, `${file_ext}.${ext}`);
const filePath = paths.join(path, `${file_ext}.${ext}`);
try {
fs.renameSync(filePath, targetPath); // Move the file synchronously
console.log(`π Moved ${file_ext}.${ext} to ${targetPath}`);
} catch (error) {
console.log(`β Error moving file ${file_ext}.${ext}:`, error.message);
}
});
processExtensions(index + 1);
});
};
processExtensions(0);
};
// Function to process extensions with customized folder names
const processExtensionsWithCustomization = (path) => {
const extensions = Object.entries(dirdata);
const processExtensions = (index) => {
if (index >= extensions.length) {
console.log('β
All files have been organized successfully with customized folder names! Closing...');
rl.close();
return;
}
const [ext, files] = extensions[index];
rl.question(`π¨ Enter new folder name for extension ".${ext}" (press Enter to keep "${ext}-files"): `, (newFolderName) => {
const folderName = newFolderName.trim() || `${ext}-files`;
fs.mkdir(paths.join(path, folderName), { recursive: true }, (error) => {
if (error) {
console.log('β Error creating folder:', error.message);
} else {
console.log(`π Folder "<${folderName}>" created!`);
}
files.map((file_ext) => {
const targetPath = paths.join(path, folderName, `${file_ext}.${ext}`);
const filePath = paths.join(path, `${file_ext}.${ext}`);
try {
fs.renameSync(filePath, targetPath); // Move the file synchronously
console.log(`π Moved ${file_ext}.${ext} to ${targetPath}`);
} catch (error) {
console.log(`β Error moving file ${file_ext}.${ext}:`, error.message);
}
});
processExtensions(index + 1);
});
});
};
processExtensions(0);
};