-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
60 lines (47 loc) · 1.61 KB
/
files.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
const fs = require('fs');
//reading files
// fs.readFile('./documents/text1.txt', (err, data) => {
// if (err) {
// console.log(err);
// }
// console.log(data.toString());
// })
//write files
// fs.writeFile('./documents/text1.txt', 'Some random text text', () => { //overwrite the file if exists
// console.log('file was written');
// })
// fs.writeFile('./assets/text1.txt', 'Some random text text', () => { //overwrite the file if exists
// console.log('file was written');
// })
// fs.writeFile('./documents/text2.txt', 'Text in the second file', () => { //create a new file
// console.log('file was written 2');
// })
// fs.writeFile('./documents/text3.txt', 'Text in the third file', () => { //create a new file
// console.log('file was written 3');
// })
//work with directories
// if (!fs.existsSync('./assets')) { //check if the folder exists and if not CREATE one
// fs.mkdir('./assets', (err) => {
// if (err) {
// console.log(err);
// }
// console.log('Folder ASSETS was created');
// })
// }
// if (fs.existsSync('./assets')) { //check if the folder exists and if so DELETE it
// fs.rmdir('./assets', (err) => {
// if (err) {
// console.log(err);
// }
// console.log('Folder was deleted');
// })
// }
//Delete files
if (fs.existsSync('./documents/deleteThisFile.txt')) {
fs.unlink('./documents/deleteThisFile.txt', (err) => {
if (err) {
console.log(err);
}
console.log('File was deleted');
})
}