Skip to content

Commit 10057c3

Browse files
authored
Merge pull request #279 from Tejas1510/directory
Added the final code for file and subdirectory counter
2 parents aecd6d4 + 5f2c677 commit 10057c3

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

Java/DirectoryLister/Readme.MD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Directory Lister
2+
3+
## Introduction
4+
```
5+
It is a simple java code which help to find number of sub directory and files inside a given directory
6+
```
7+
8+
9+
## How to use the code
10+
```
11+
1. Download the given code
12+
2. Run the directory.java file
13+
3. Enter the path of your Folder
14+
3. The number of files and sub directories in the entered directory will be shown as output
15+
```
16+
## Output
17+
18+
![endpoint](https://github.com/Tejas1510/hacking-tools-scripts/blob/directory/Java/DirectoryLister/images/image1.png)
19+
20+
![built with love](https://forthebadge.com/images/badges/built-with-love.svg)
21+
22+
Check out my Github profile [Tejas1510!](https://github.com/Tejas1510)

Java/DirectoryLister/directory.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package FileCounter;
2+
import java.io.File;
3+
import java.util.*;
4+
// The time and space complexity is O(n) as we will be iterating through all files recursively
5+
public class filecounter {
6+
public static int countDirectory=0;
7+
// function which counts number of file and sub-directory in the given directory
8+
public static int countFilesInDirectory(File directory){
9+
int countFile=0;
10+
// iterate over all files
11+
for (File file : directory.listFiles()) {
12+
// if it is file increment countFile
13+
if (file.isFile()) {
14+
countFile++;
15+
}
16+
// else increment countDirectory
17+
if (file.isDirectory()) {
18+
countDirectory++;
19+
countFile += countFilesInDirectory(file);
20+
}
21+
}
22+
return countFile;
23+
}
24+
public static void main(String args[]) {
25+
Scanner s = new Scanner(System.in);
26+
// Take input as the path for directory
27+
System.out.println("Enter the Path of your directory : ");
28+
String path = s.next();
29+
// Make a file object and pass the entered path as an argument
30+
File file = new File(path);
31+
int countFile = filecounter.countFilesInDirectory(file);
32+
System.out.println("The No of Files in entered Directory are : "+countFile);
33+
System.out.println("The No of Sub-Directory in entered Directory are : "+countDirectory);
34+
}
35+
}
21.8 KB
Loading

0 commit comments

Comments
 (0)