From 8d4ac148e585484d8f3ba38795993f59b8e50ddb Mon Sep 17 00:00:00 2001 From: mateenmohsin Date: Sun, 3 Jun 2018 23:24:04 +0500 Subject: [PATCH 1/3] Folder analyser --- folder-analyzer/.gitignore | 2 + folder-analyzer/README.md | 62 +----------- folder-analyzer/analyser.js | 161 ++++++++++++++++++++++++++++++ folder-analyzer/package-lock.json | 40 ++++++++ folder-analyzer/package.json | 20 ++++ 5 files changed, 228 insertions(+), 57 deletions(-) create mode 100644 folder-analyzer/.gitignore create mode 100644 folder-analyzer/analyser.js create mode 100644 folder-analyzer/package-lock.json create mode 100644 folder-analyzer/package.json diff --git a/folder-analyzer/.gitignore b/folder-analyzer/.gitignore new file mode 100644 index 0000000..dff2f02 --- /dev/null +++ b/folder-analyzer/.gitignore @@ -0,0 +1,2 @@ +node_modules* +node_modules/* \ No newline at end of file diff --git a/folder-analyzer/README.md b/folder-analyzer/README.md index 72c5afc..33c4244 100644 --- a/folder-analyzer/README.md +++ b/folder-analyzer/README.md @@ -1,60 +1,8 @@ -# Folder Analyzer - -Goal of this program is to analyze a *folder structure* (folder and it's subfolder hierarchy) on your computer's filesystem in order to retrieve various statistics about the files contained. The main purpose of the program is to categorize the files which are scanned into these categories: -- images -- documents -- music -- videos -- other files - -The classification of a file should be done based on it's file extension. The tables below specify each of the categories and the connected file extensions. The output of the program shall be something like - -found 7347 files in c:\root within 34 folders with -43% images (728 files) -30% documents (2203 files) -2% audio (147 files) -15% videos (1101 files) -10% other (734 files) - -## File Categories - -| Category | File Associations | -| ---------|:-----------------------------------------:| -| image | jpeg, jpg, png, gif, svg | -| document | doc, docx, pdf, html, odt, ppt, pptx, txt | -| audio | mp3, wav, aac, wma | -| video | mp4, mpg, avi, wmv | - -Note that there are much more file extensions for each category. As this requirement just serves educational purposes, it is not complete. - -For further information look here:\ -https://en.wikipedia.org/wiki/Video_file_format -https://en.wikipedia.org/wiki/Audio_file_format -https://en.wikipedia.org/wiki/Audio_file_format -https://blog.filestack.com/thoughts-and-knowledge/document-file-extensions-list/ - -## Program usage - -The idea is to use the program on the commandline like this: - -``` -analyze-folders -``` - -Then the specified output shall be generated. - -## Deliverables - -Fork this repository and put your code inside this root folder. - -Write a program in the language of your choice which fulfills this requirement. Make sure, that you create small units of execution in order for later reusage. Also try to comment as much as possible your code, if something is not obvious. - -Feel free to deliver more functionality, if you like. Some ideas here: -- statistics based on file types (distribution of image file extensions among the image files) -- implement switch to also consider hidden files -- also count the file size and build an alternative statistic based on file size and not amount of files - -Update the description here with information on howto test the program. +## Folder Analyzer +Clone the github repo. +### How To Run +npm install +node analyser.js ["path of directory"] \ No newline at end of file diff --git a/folder-analyzer/analyser.js b/folder-analyzer/analyser.js new file mode 100644 index 0000000..2ec0684 --- /dev/null +++ b/folder-analyzer/analyser.js @@ -0,0 +1,161 @@ +const path = require('path') +const fs = require('fs'); + + +//==variables to count the files==// + +let totalFiles = 0; +let dir = 0; +let dirSize = 0; +const mbConst = 1048576; + +const results = { + 'imageFiles': {count: 0, size :0}, + 'documentFiles': {count: 0, size: 0}, + 'audioFiles': {count: 0, size: 0}, + 'videoFiles': {count: 0, size: 0}, + 'otherFiles': {count: 0, size: 0} +}; + + +//==Start of Method to find percentage==// + +function findPercentage(currentValue, totalValue){ + + return Math.round((currentValue/totalValue)*100) +} + +//==End of Method to find percentage==// + + +//==Start of Method to convert Bytes to Mbs==// + +function convertBytesToMb(value){ + + var result = Math.trunc(value/mbConst); + if(result <= 0){ + return "less than 1"; + } + else{ + return result; + } +} + +//==End of Method to convert Bytes to Mbs==// + + +//==Start of Method to update file counter==// + +function updateFileCounter(filename, fileStatus){ + totalFiles++; + switch(path.extname(filename)){ //switch to check the file type and incrementing counter accordingly. + case '.jpeg': + case '.jpg' : + case '.png' : + case '.gif' : + case '.svg' : + results['imageFiles'].count++; + results['imageFiles'].size += fileStatus.size; + break; + + case '.doc': + case '.docs': + case '.pdf': + case '.html': + case '.odt': + case '.ppt': + case '.pptx': + case '.txt': + results['documentFiles'].count++; + results['documentFiles'].size += fileStatus.size; + break; + + case '.mp3': + case '.wav': + case '.aac': + case '.wma': + results['audioFiles'].count++; + results['audioFiles'].size += fileStatus.size; + break; + + case '.mp4': + case '.mpg': + case '.avi': + case '.wmv': + results['videoFiles'].count++; + results['videoFiles'].size += fileStatus.size; + break; + + default: + results['otherFiles'].count++; + results['otherFiles'].size += fileStatus.size; + break; + } +} + + + +//==End of Method to update file counter==// + + + +//==Start of Method to analyze the directory==// + +function analyzingDir(startPath){ + + const files = fs.readdirSync(startPath); //reading the files in that directory. + let filename = null; + let fileStatus = null; + + files.forEach((file) => { + filename=path.join(startPath,file); //joining the path + filename. + fileStatus = fs.lstatSync(filename); //getting status of joined file. + + dirSize += fileStatus.size; //adding all files size + + if (fileStatus.isDirectory()){ //checking wheather it is a diretory? then incrementing the countDirectory + //and passing the whole path again to the Analyzing method. + dir++; //incrementing diretory count + analyzingDir(filename); //recursive call + } + else{ + updateFileCounter(filename, fileStatus); //else if its a file, update file counter is called + }; + }); +}; + + +//==End of Method to analyze the directory==// + + + +//==Start of Main Method RUN ==// + +function run(currentPath){ + + const startTime =new Date().getTime(); //getting Start time of analyzing + analyzingDir(currentPath); //calling analyzing method + const endTime =new Date().getTime(); //getting end time of analyzing + + console.log("\nProcess Time in millisecond: ",endTime-startTime); + console.log(`Found ${totalFiles} files in ${currentPath} within ${dir} folders of size ${convertBytesToMb(dirSize)}mb with`); + + Object.keys(results).forEach((key) => { + console.log(`${findPercentage(results[key].count,totalFiles)}% ${key} of size ${convertBytesToMb(results[key].size)}mb (${results[key].count} files)`); + }); +} + +//==End of Main Method RUN ==// + + + +const currentPath = process.argv[2]; +if (!fs.existsSync(currentPath)){ //if didn't find this file path it will return with "no such dir". + console.log("No such dir :",currentPath); + return; +} + + +run(currentPath); //if path exists then run main function + + diff --git a/folder-analyzer/package-lock.json b/folder-analyzer/package-lock.json new file mode 100644 index 0000000..8f5ef82 --- /dev/null +++ b/folder-analyzer/package-lock.json @@ -0,0 +1,40 @@ +{ + "name": "folder-analyzer", + "version": "0.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" + }, + "inherits": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz", + "integrity": "sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=" + }, + "path": { + "version": "0.12.7", + "resolved": "https://registry.npmjs.org/path/-/path-0.12.7.tgz", + "integrity": "sha1-1NwqUGxM4hl+tIHr/NWzbAFAsQ8=", + "requires": { + "process": "0.11.10", + "util": "0.10.3" + } + }, + "process": { + "version": "0.11.10", + "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", + "integrity": "sha1-czIwDoQBYb2j5podHZGn1LwW8YI=" + }, + "util": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", + "integrity": "sha1-evsa/lCAUkZInj23/g7TeTNqwPk=", + "requires": { + "inherits": "2.0.1" + } + } + } +} diff --git a/folder-analyzer/package.json b/folder-analyzer/package.json new file mode 100644 index 0000000..7e5b5d6 --- /dev/null +++ b/folder-analyzer/package.json @@ -0,0 +1,20 @@ +{ + "name": "folder-analyzer", + "version": "0.0.1", + "description": "Analyzing this files in folder", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/mateenmohsin/coding-challenges.git" + }, + "author": "mateen.mohsin@hotmail.com", + "license": "ISC", + "homepage": "https://github.com/mateenmohsin/coding-challenges#readme", + "dependencies": { + "fs": "0.0.1-security", + "path": "^0.12.7" + } +} From 9435b1229876e031e42b7330eaa9b19668bb76c6 Mon Sep 17 00:00:00 2001 From: mateenmohsin Date: Tue, 5 Jun 2018 22:25:52 +0500 Subject: [PATCH 2/3] Updated the checks for User's Read Permission --- folder-analyzer/analyser.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/folder-analyzer/analyser.js b/folder-analyzer/analyser.js index 2ec0684..fb8725a 100644 --- a/folder-analyzer/analyser.js +++ b/folder-analyzer/analyser.js @@ -1,4 +1,4 @@ -const path = require('path') +const path = require('path'); const fs = require('fs'); @@ -103,6 +103,10 @@ function updateFileCounter(filename, fileStatus){ function analyzingDir(startPath){ + + + try{ + const files = fs.readdirSync(startPath); //reading the files in that directory. let filename = null; let fileStatus = null; @@ -122,6 +126,14 @@ function analyzingDir(startPath){ updateFileCounter(filename, fileStatus); //else if its a file, update file counter is called }; }); + + + } + catch(error){ + console.log("Note: Please give the Read Permission to this directory structure,\nor try with the user who has Read Permission to this directory structure!"); + process.exit(); +} + }; @@ -137,12 +149,16 @@ function run(currentPath){ analyzingDir(currentPath); //calling analyzing method const endTime =new Date().getTime(); //getting end time of analyzing + //==Displaying output==// + console.log("\nProcess Time in millisecond: ",endTime-startTime); console.log(`Found ${totalFiles} files in ${currentPath} within ${dir} folders of size ${convertBytesToMb(dirSize)}mb with`); Object.keys(results).forEach((key) => { console.log(`${findPercentage(results[key].count,totalFiles)}% ${key} of size ${convertBytesToMb(results[key].size)}mb (${results[key].count} files)`); }); + + //====================// } //==End of Main Method RUN ==// From 9ce436deb48d03a6afdcfe41546752c6153da417 Mon Sep 17 00:00:00 2001 From: mateenmohsin Date: Wed, 6 Jun 2018 11:30:27 +0500 Subject: [PATCH 3/3] update on read Permission check --- folder-analyzer/analyser.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/folder-analyzer/analyser.js b/folder-analyzer/analyser.js index fb8725a..f7b23fa 100644 --- a/folder-analyzer/analyser.js +++ b/folder-analyzer/analyser.js @@ -7,6 +7,7 @@ const fs = require('fs'); let totalFiles = 0; let dir = 0; let dirSize = 0; +let checkDirCount = 0; const mbConst = 1048576; const results = { @@ -107,6 +108,7 @@ function analyzingDir(startPath){ try{ + checkDirCount++; const files = fs.readdirSync(startPath); //reading the files in that directory. let filename = null; let fileStatus = null; @@ -130,8 +132,15 @@ function analyzingDir(startPath){ } catch(error){ - console.log("Note: Please give the Read Permission to this directory structure,\nor try with the user who has Read Permission to this directory structure!"); - process.exit(); + + if(checkDirCount > 1){ + dir--; + console.log("\nPermission to this Directory Structure is denied : "+error.path); + } + else{ + console.log("Note: Please give the Read Permission to this directory structure,\nor try with the user who has Read Permission to this directory structure!"); + process.exit(); + } } };