-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_files_streams.js
More file actions
90 lines (68 loc) · 1.92 KB
/
Copy path11_files_streams.js
File metadata and controls
90 lines (68 loc) · 1.92 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
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
const fs = require('fs');
console.log('starting reading')
const files1 = fs.readdirSync('./node_modules')
//make the function load synchronously
fs.readdir('./node_modules', (err, files) => {
if(err)
throw `directory doesn't exit`
// console.log('node_modules', files)
console.log('stop reading node_modules')
})
const files2 = fs.readdirSync('./server')
console.log('stop reading')
// console.log(files1) // return the number of folders in an array
// console.log(files2) // return the number of files in an array
/**
* Read Content of the files with fs.module
*/
const text = fs.readFileSync('./assets/json/data.json', "utf-8")
console.log('text', text)
console.log('reading content of file stop')
/**
* Important : If we want to read binary files, we don't supply the encoding
*/
/**
* Write File method
*/
const md = `
# This is auto generated files create with the help of NodeJS
hello
`
fs.writeFile('./assets/nodes1.md', md.trim(), err => {
if(err)
throw err
console.log('file generated')
})
/**
* Write directory
*/
const directoryName = './assets/files';
if(!fs.existsSync(directoryName)) { //check whether directory exist or not
fs.mkdir(directoryName, err => {
if(err)
throw err
console.log('directory generated')
})
} else {
console.log('directory exist! :(')
}
/**
* appendFile
*/
const jsonFile = require('./assets/json/data.json')
fs.appendFile(`${directoryName}/krishna.md`, `${jsonFile.name}`, err => {
if(err){
throw err
}
})
fs.renameSync(`${directoryName}/krishna.md`, `${directoryName}/data.md`)
setTimeout(() => {
//empty directory before removing
fs.readdirSync(directoryName).forEach(fileName => {
console.log('fileName', fileName)
// unlink is use to delete the files
fs.unlinkSync(`${directoryName}/${fileName}`)
})
//remove directory
fs.rmdirSync(directoryName)
}, 5000)