File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments