Skip to content

Commit 4a2bfe7

Browse files
added filesearch
1 parent bd906bd commit 4a2bfe7

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

file_search/README.md

Lines changed: 32 additions & 0 deletions
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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import os
2+
import datetime
3+
import argparse
4+
from progress.bar import Bar
5+
6+
class FileSearch:
7+
def __init__(self):
8+
self.file_count = 0
9+
self.progress_bar = Bar('Searching', suffix='%(eta)ds')
10+
11+
def get_file_count(self, path, is_folder):
12+
13+
if not is_folder:
14+
print("[INFO] Reading files ...")
15+
else:
16+
print("[INFO] Reading folders ...")
17+
18+
self.file_count = 0
19+
for root, dirs, files in os.walk(path):
20+
if is_folder:
21+
self.file_count += len(dirs)
22+
else:
23+
self.file_count += len(files)
24+
25+
def __recursive_check(self, search_term, start_dir, is_folder, disable_case, show_bar):
26+
results = []
27+
for root, dirs, files in os.walk(start_dir):
28+
if is_folder:
29+
for dir in dirs:
30+
31+
if show_bar:
32+
self.progress_bar.next()
33+
34+
if disable_case and search_term in dir:
35+
results.append(os.path.join(root, dir))
36+
elif not disable_case and search_term.lower() in dir.lower():
37+
results.append(os.path.join(root, dir))
38+
else:
39+
for file in files:
40+
41+
if show_bar:
42+
self.progress_bar.next()
43+
44+
if disable_case and search_term in file:
45+
results.append(os.path.join(root, file))
46+
elif not disable_case and search_term.lower() in file.lower():
47+
results.append(os.path.join(root, file))
48+
49+
return results
50+
51+
def search(self, search_term, start_dir, is_folder=False, disable_case=False, show_bar=True):
52+
self.file_count = 0
53+
self.get_file_count(start_dir, is_folder)
54+
self.progress_bar.max = self.file_count
55+
56+
if not os.path.exists(start_dir):
57+
raise Exception(f"Path {start_dir} does not exist.")
58+
59+
start_time = datetime.datetime.now()
60+
result = self.__recursive_check(search_term, start_dir, is_folder, disable_case, show_bar)
61+
end_time = datetime.datetime.now()
62+
63+
if not is_folder:
64+
print(f"\n\n[INFO] Searched {self.file_count:,} files in {(end_time-start_time).total_seconds()} seconds")
65+
else:
66+
print(f"\n\n[INFO] Searched {self.file_count:,} folders in {(end_time-start_time).total_seconds()} seconds")
67+
68+
return result
69+
70+
if __name__ == "__main__":
71+
72+
parser = argparse.ArgumentParser(description="Search files within your computer")
73+
74+
parser.add_argument("search-term", metavar="term", type=str, help="Filename/Word to search for")
75+
parser.add_argument("start-dir", metavar="dir", type=str, help="Directory where you want to search")
76+
77+
parser.add_argument("-dc", "--disable-case", action="store_true", help="Perform case insensitive search")
78+
parser.add_argument("-f", "--folder", action="store_true", help="Search only folders (default: searches files)")
79+
parser.add_argument("-nb", "--no-bar", action="store_false", help="Not show progress bar")
80+
81+
args = parser.parse_args().__dict__
82+
83+
searcher = FileSearch()
84+
results = searcher.search(args['search-term'], args['start-dir'], args['folder'], args['disable_case'], args['no_bar'])
85+
86+
print("[INFO] Results: \n")
87+
for path in results:
88+
print("Path:", path)
89+
print("")

file_search/requirements.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)