-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (36 loc) · 1.06 KB
/
script.js
File metadata and controls
43 lines (36 loc) · 1.06 KB
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
const fs = require("fs");
function listDirectory(path) {
try {
return fs.readdirSync(path);
} catch (error) {
return error.message;
}
}
function saveToJson(data, filename) {
try {
fs.writeFileSync(filename, JSON.stringify(data, null, 4));
return true;
} catch (error) {
return error.message;
}
}
function main() {
const directoryPath = "/home/emmanuel/Desktop/Curso-JS-Moderno"; // Replace with a directory path in your case
const contents = listDirectory(directoryPath);
if (Array.isArray(contents)) {
console.log("Contenidos del directorio:", contents);
// Opción 1: Guardar en un archivo JSON
const jsonFilename = "contenido_directorio.json";
if (saveToJson(contents, jsonFilename)) {
console.log(`Contenidos guardados en '${jsonFilename}'`);
} else {
console.log("Error al guardar en JSON.");
}
// Opción 2: Almacenar en un arreglo en memoria
const inMemoryArray = contents;
console.log("Contenidos en memoria:", inMemoryArray);
} else {
console.log("Error:", contents);
}
}
main();