Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folder analyser #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions folder-analyzer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules*
node_modules/*
62 changes: 5 additions & 57 deletions folder-analyzer/README.md
Original file line number Diff line number Diff line change
@@ -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 <directory>
```

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"]
186 changes: 186 additions & 0 deletions folder-analyzer/analyser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
const path = require('path');
const fs = require('fs');


//==variables to count the files==//

let totalFiles = 0;
let dir = 0;
let dirSize = 0;
let checkDirCount = 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){



try{

checkDirCount++;
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
};
});


}
catch(error){

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();
}
}

};


//==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

//==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 ==//



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


40 changes: 40 additions & 0 deletions folder-analyzer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions folder-analyzer/package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"license": "ISC",
"homepage": "https://github.com/mateenmohsin/coding-challenges#readme",
"dependencies": {
"fs": "0.0.1-security",
"path": "^0.12.7"
}
}