1
+ let inputArr = process . argv . slice ( 2 ) ;
2
+ let fs = require ( "fs" ) ;
3
+ let path = require ( "path" ) ;
4
+ // console.log(inputArr);
5
+ // node fileSystemOrganizer.js tree "directoryPath"
6
+ // node fileSystemOrganizer.js organize "directoryPath"
7
+ // node fileSystemOrganizer.js help
8
+
9
+ let types = {
10
+ archives : [ "zip" , "7z" , "rar" , "tar" , "gz" , "ar" , "iso" , "xz" ] ,
11
+ media : [ "mp4" , "mkv" , "mp3" ] ,
12
+ documents : [ "docx" , "doc" , "pdf" , "xlsx" , "xls" , "odt" , "ods" , "odg" , "odf" , "txt" , "ps" , "tex" , "csv" ] ,
13
+ app : [ "exe" , "dmg" , "pkg" , "deb" , "apk" ]
14
+ }
15
+
16
+ let command = inputArr [ 0 ] ;
17
+ switch ( command ) {
18
+ case "tree" :
19
+ treeFn ( inputArr [ 1 ] )
20
+ break ;
21
+ case "organize" :
22
+ organizeFn ( inputArr [ 1 ] )
23
+ break ;
24
+ case "help" :
25
+ helpFn ( )
26
+ break ;
27
+ default :
28
+ console . log ( "Please input a valid command or type \"node fileSystemOrganizer.js help\" " )
29
+ break ;
30
+ }
31
+
32
+ function treeFn ( dirPath ) {
33
+ if ( dirPath == undefined ) {
34
+ treeMaker ( process . cwd ( ) , "" ) ;
35
+ return ;
36
+ } else if ( ! fs . existsSync ( dirPath ) ) {
37
+ console . log ( "Please enter a valid path!" )
38
+ } else {
39
+ treeMaker ( dirPath , "" ) ;
40
+ console . log ( "Tree command implemented for " , dirPath ) ;
41
+ }
42
+ }
43
+
44
+ function treeMaker ( dirPath , intend ) {
45
+ if ( fs . lstatSync ( dirPath ) . isFile ( ) ) {
46
+ console . log ( intend + "├───" + path . basename ( dirPath ) ) ;
47
+ } else if ( fs . lstatSync ( dirPath ) . isDirectory ( ) ) {
48
+ console . log ( intend + "└───" + path . basename ( dirPath ) ) ;
49
+ let children = fs . readdirSync ( dirPath ) ;
50
+ for ( let i = 0 ; i < children . length ; i ++ ) {
51
+ let childPath = path . join ( dirPath , children [ i ] ) ;
52
+ treeMaker ( childPath , intend + "\t" ) ;
53
+ }
54
+ }
55
+ }
56
+
57
+ function organizeFn ( dirPath ) {
58
+ if ( dirPath == undefined ) {
59
+ console . log ( "You must enter a path!" ) ;
60
+ return ;
61
+ } else if ( ! fs . existsSync ( dirPath ) ) {
62
+ console . log ( "Please enter a valid path!" )
63
+ } else {
64
+ let destPath = path . join ( dirPath , "organized_files" ) ;
65
+ if ( ! fs . existsSync ( destPath ) ) {
66
+ fs . mkdirSync ( destPath ) ;
67
+ }
68
+ organizer ( dirPath , destPath ) ;
69
+ console . log ( "Organize command implemented for" , dirPath ) ;
70
+ }
71
+ }
72
+
73
+ function organizer ( src , dest ) {
74
+ let childs = fs . readdirSync ( src ) ; //reads path and lists the files and folders
75
+ for ( let i = 0 ; i < childs . length ; i ++ ) {
76
+ let childAddress = path . join ( src , childs [ i ] ) ;
77
+ if ( fs . lstatSync ( childAddress ) . isFile ( ) ) { //lstatSync() sends info about file/dir. isFile() checks if it is a file
78
+ let category = getCategory ( childs [ i ] ) ;
79
+ moveFiles ( childAddress , dest , category ) ;
80
+ // console.log("Moved", childAddress, "to", dest);
81
+ } else if ( fs . lstatSync ( childAddress ) . isDirectory ( ) && childAddress != dest ) { //isDirectory() check if it is a folder ......... childAddress!=dest makes sure that after organizing <folder1> in the given <src> it will not move to <organized_files> and start organizing it again, i.e. moving it to current path(no change) and then deleting the files. So in the end, it will just delete already organized files from <folder1>
82
+ // console.log(childAddress)
83
+ organizer ( childAddress , dest ) ;
84
+ // console.log(childAddress)
85
+ fs . rmdirSync ( childAddress ) ;
86
+ }
87
+ }
88
+ }
89
+
90
+ function moveFiles ( srcFile , dest , category ) {
91
+ let categoryPath = path . join ( dest , category ) ;
92
+ if ( ! fs . existsSync ( categoryPath ) ) {
93
+ fs . mkdirSync ( categoryPath ) ;
94
+ }
95
+ let filename = path . basename ( srcFile ) ;
96
+ let destFilePath = path . join ( categoryPath , filename ) ;
97
+ fs . copyFileSync ( srcFile , destFilePath ) ; //copies file
98
+ fs . unlinkSync ( srcFile ) ; // deletes file
99
+ }
100
+
101
+ function getCategory ( name ) {
102
+ let ext = path . extname ( name ) ;
103
+ ext = ext . slice ( 1 ) ;
104
+ for ( let type in types ) {
105
+ let cTypeArray = types [ type ] ;
106
+ for ( let i = 0 ; i < cTypeArray . length ; i ++ ) {
107
+ if ( ext == cTypeArray [ i ] ) {
108
+ return type ;
109
+ }
110
+ }
111
+ }
112
+ return "others" ;
113
+ }
114
+
115
+ function helpFn ( ) {
116
+ console . log ( `
117
+ List of all the commands:
118
+ node fileSystemOrganizer.js tree <directoryPath>
119
+ node fileSystemOrganizer.js organize <directoryPath></directoryPath>
120
+ ` ) ; // backtic " ` " shows the output in the same format/line/spacing as written in the code just like ''' in python
121
+ }
0 commit comments