-
Notifications
You must be signed in to change notification settings - Fork 0
/
fflist.py
178 lines (151 loc) · 4.16 KB
/
fflist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from __future__ import print_function
from os import scandir, walk, system
from argparse import ArgumentParser
from pathlib import Path
import asyncio
import platform
# saving all the gathered filepaths into list
total_files = []
global printout
# get os name
def get_os_name() -> str:
return platform.system()
# clear screen
def sclear() -> None:
plt = get_os_name()
if plt == "Windows":
system('cls')
elif plt in ["Linux", "Darwin"]:
system('clear')
# convert names to path
async def convert_to_path(dirpath: str):
dirpath.encode('unicode escape')
p = Path(dirpath)
return p
# progress bar
async def pbar(progress, total: float) -> None:
try:
percent = 100 * (progress / float(total))
bar = '+' * int(percent) + '-' * (100 - int(percent))
print(f"\r|{bar}| {percent:.2f}%", end="\r")
except ZeroDivisionError as e:
print(f"No Folders Found\n\nERROR: {e}")
# read lines from files
def readfilelines(filepath: str) -> []:
try:
with open(filepath, 'r') as f:
lines = [x.rstrip() for x in f.readlines()]
return lines
except Exception as e:
raise e
# print output
async def print_output():
sclear()
fpath = str(Path.cwd()) + "/files.txt"
fpath = await convert_to_path(fpath)
print(readfilelines(fpath))
# save the list into file
async def save_files() -> None:
ffolder = str(Path.cwd()) + "/files.txt"
filepath = await convert_to_path(ffolder)
with open(filepath, 'w') as f:
for file in total_files:
try:
f.write(f"{file}\n")
except Exception as e:
pass
if printout == True:
await print_output()
# get the files from folder
async def get_files(dirpath: str) -> []:
files = []
f_obj = scandir(path=dirpath)
for entry in f_obj:
if entry.is_file() and entry.name != None:
ffolder = f"{dirpath}\\{entry.name}"
fpath = await convert_to_path(ffolder)
files.append(fpath)
return files
# total appender
async def total_appender(dirpath: str) -> None:
total_lists = []
total_lists.append(await get_files(dirpath))
for lst in total_lists:
for file in lst:
total_files.append(file)
# init
async def init(init_dir: str, isDeep: bool) -> None:
sclear()
init_dir = await convert_to_path(init_dir)
print(f"Gathering FileSystem Directories from {init_dir}..")
dirs_raw = []
match isDeep:
case True:
filenum = 0
try:
for x in walk(init_dir):
dirs_raw.append(x[0])
filenum += 1
if filenum % 100 == 0:
print(f"Folders Gathered: {filenum}")
except Exception as e:
raise e
case False:
try:
dirs_raw = [x[0] for x in walk(init_dir)]
except Exception as e:
raise e
print(f"Gathering files in {len(dirs_raw)} directories..")
await pbar(0, len(dirs_raw))
n = 0
while n <= len(dirs_raw)-1:
dirpath = await convert_to_path(dirs_raw[n])
await total_appender(dirpath)
n += 1
await pbar(n, len(dirs_raw))
print(f"Saving file list...")
await save_files()
# read config file
async def main() -> None:
global printout
sclear()
parser = ArgumentParser(description="FFlist Argument Parser", add_help=True)
folder_group = parser.add_mutually_exclusive_group()
folder_group.add_argument('-dir','--directory', type=str, help="User Defined Folder")
folder_group.add_argument('-r','--read', type=str, help="User Defined Folders From File [EX: dirs.txt]")
folder_group.add_argument('-full','--full', action="store_true", help="All System Files")
parser.add_argument('-print','--print', action="store_true", help="Print Output")
args = parser.parse_args()
if args.print:
printout = True
else:
printout = False
if args.directory:
if args.directory != None:
try:
asyncio.gather(init(args.directory, False))
except Exception as e:
raise e
if args.read:
read_tasks = []
if args.read != None:
dirs = readfilelines(args.read)
for _dir in dirs:
read_tasks.append(asyncio.create_task(init(_dir, False)))
runs = await asyncio.gather(*read_tasks, return_exceptions=True)
if args.full:
plt = get_os_name()
sysdir = ""
match plt:
case "Windows":
sysdir = "C:/"
case "Linux":
sysdir = "/home/"
case "Darwin":
sysdir = "/home/"
try:
asyncio.gather(init(sysdir, True))
except Exception as e:
raise e
if __name__ == "__main__":
asyncio.run(main())