Skip to content

Commit 30b4d02

Browse files
authored
Merge pull request #859 from visheshdvivedi/filesearch-branch
Add script to search files in the folder
2 parents aa5a4b2 + acc3b5c commit 30b4d02

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

file_search/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# File Search
2+
3+
Automation script to search any file/folder inside any directory within your computer
4+
5+
Just run the following command to search
6+
```
7+
python filesearch.py "search_term" "path/to/folder"
8+
```
9+
10+
To go through all the available options, type
11+
```
12+
python filesearch.py -h
13+
```
14+
15+
You will get options like this:
16+
```
17+
usage: filesearch.py [-h] [-dc] [-f] [-nb] term dir
18+
19+
Search files within your computer
20+
21+
positional arguments:
22+
term Filename/Word to search for
23+
dir Directory where you want to search
24+
25+
options:
26+
-h, --help show this help message and exit
27+
-dc, --disable-case Perform case insensitive search
28+
-f, --folder Search only folders (default: searches files)
29+
-nb, --no-bar Not show progress bar
30+
```
31+
32+
##### Contributed By: [visheshdvivedi](https://github.com/visheshdvivedi)

file_search/filesearch.py

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import os
2+
import datetime
3+
import argparse
4+
from progress.bar import Bar
5+
6+
7+
class FileSearch:
8+
def __init__(self):
9+
self.file_count = 0
10+
self.progress_bar = Bar('Searching', suffix='%(eta)ds')
11+
12+
def get_file_count(self, path, is_folder):
13+
14+
if not is_folder:
15+
print("[INFO] Reading files ...")
16+
else:
17+
print("[INFO] Reading folders ...")
18+
19+
self.file_count = 0
20+
for root, dirs, files in os.walk(path):
21+
if is_folder:
22+
self.file_count += len(dirs)
23+
else:
24+
self.file_count += len(files)
25+
26+
def __perform_search(
27+
self,
28+
search_term,
29+
start_dir,
30+
is_folder,
31+
disable_case,
32+
show_bar
33+
):
34+
results = []
35+
for root, dirs, files in os.walk(start_dir):
36+
if is_folder:
37+
for dir in dirs:
38+
39+
if show_bar:
40+
self.progress_bar.next()
41+
42+
if disable_case and search_term in dir:
43+
results.append(os.path.join(root, dir))
44+
elif not disable_case and search_term.lower() in dir.lower():
45+
results.append(os.path.join(root, dir))
46+
else:
47+
for file in files:
48+
49+
if show_bar:
50+
self.progress_bar.next()
51+
52+
if disable_case and search_term in file:
53+
results.append(os.path.join(root, file))
54+
elif not disable_case and search_term.lower() in file.lower():
55+
results.append(os.path.join(root, file))
56+
57+
return results
58+
59+
def search(
60+
self,
61+
search_term,
62+
start_dir,
63+
is_folder=False,
64+
disable_case=False,
65+
show_bar=True
66+
):
67+
self.file_count = 0
68+
self.get_file_count(start_dir, is_folder)
69+
self.progress_bar.max = self.file_count
70+
71+
if not os.path.exists(start_dir):
72+
raise Exception(f"Path {start_dir} does not exist.")
73+
74+
start_time = datetime.datetime.now()
75+
result = self.__perform_search(
76+
search_term,
77+
start_dir,
78+
is_folder,
79+
disable_case,
80+
show_bar
81+
)
82+
end_time = datetime.datetime.now()
83+
84+
if not is_folder:
85+
print(
86+
f"\n\n[INFO] Searched {self.file_count:,} files in {(end_time-start_time).total_seconds()} seconds")
87+
else:
88+
print(
89+
f"\n\n[INFO] Searched {self.file_count:,} folders in {(end_time-start_time).total_seconds()} seconds")
90+
91+
return result
92+
93+
94+
if __name__ == "__main__":
95+
96+
parser = argparse.ArgumentParser(
97+
description="Search files within your computer")
98+
99+
parser.add_argument(
100+
"search-term",
101+
metavar="term",
102+
type=str,
103+
help="Filename/Word to search for"
104+
)
105+
parser.add_argument(
106+
"start-dir",
107+
metavar="dir",
108+
type=str,
109+
help="Directory where you want to search"
110+
)
111+
112+
parser.add_argument(
113+
"-dc",
114+
"--disable-case",
115+
action="store_true",
116+
help="Perform case insensitive search"
117+
)
118+
parser.add_argument(
119+
"-f",
120+
"--folder",
121+
action="store_true",
122+
help="Search only folders (default: searches files)"
123+
)
124+
parser.add_argument(
125+
"-nb",
126+
"--no-bar",
127+
action="store_false",
128+
help="Not show progress bar"
129+
)
130+
131+
args = parser.parse_args().__dict__
132+
133+
searcher = FileSearch()
134+
results = searcher.search(
135+
args['search-term'],
136+
args['start-dir'],
137+
args['folder'],
138+
args['disable_case'],
139+
args['no_bar']
140+
)
141+
142+
print("[INFO] Results: \n")
143+
for path in results:
144+
print("Path:", path)
145+
print("")

file_search/requirements.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)