-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (151 loc) · 6.58 KB
/
main.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
import os
from termcolor import colored
from datetime import datetime
from time import sleep
from sys import exit
from ctypes import windll
import argparse
import zlib
import json
now = datetime.now()
dt = now.strftime("%d/%m/%Y %H:%M:%S")
dtds = now.strftime("%d-%m-%Y %H-%M-%S")
version = "1.0.0"
files_list = []
dirs_list = []
hash_list = []
parser = argparse.ArgumentParser(prog="GetFileList.exe", usage='%(prog)s [options]')
parser.add_argument('-p', '--path',
dest='path_to_target',
default="simple",
help='Path to Target',
type=str
)
parser.add_argument('-f', '--format',
dest='file_format',
default="plain",
help='output format. plain & json supported',
type=str
)
parser.add_argument('-hf', '--hash',
dest='calculate_hashes',
default="False",
help='calculate the hashes for all files. CRC32 only currently',
type=str
)
parser.add_argument('-ext', '--extention',
dest='file_ending',
default="log",
help='choose a prefered output extension e.g. log, txt',
type=str
)
args = parser.parse_args()
seperator = "==================================\n"
if os.name == "nt":
os.system('cls')
windll.kernel32.SetConsoleTitleW("Husko's GetFileList | v" + version)
print(colored("======================================================================================================================", "red"))
print(colored("| |", "red"))
print(colored("| " + colored("Product: ", "white") + colored("GetFileList","green") + colored(" |", "red"), "red"))
print(colored("| " + colored("Version: ", "white") + colored(version,"green") + colored(" |", "red"), "red"))
print(colored("| " + colored("Description: ", "white") + colored("Get all files & folders from a directory fast","green") + colored(" |", "red"), "red"))
print(colored("| |", "red"))
print(colored("======================================================================================================================", "red"))
print("")
if args.path_to_target == "simple":
print("Please enter the path to target folder.")
target = input(">> ")
else:
target = args.path_to_target
if not os.path.join(target):
print(colored("Invalid Path Provided!", "red"))
sleep(2)
folder = os.listdir(target)
for files in folder:
if os.path.isfile(target + "/" + files):
if args.calculate_hashes == "CRC32":
prev = 0
for eachLine in open(target + "/" + files,"rb"):
prev = zlib.crc32(eachLine, prev)
hash = "%X"%(prev & 0xFFFFFFFF)
hash_list.append(hash)
files_list.append(files)
elif os.path.isdir(target + "/" + files):
dirs_list.append(files)
if args.file_format != "json":
with open("GetFileList Report - " + dtds + f".{args.file_ending}", "w") as file:
file.write(seperator)
file.write("Generated using GetFileList by Husko ~ The right way to get & share a file list.\n")
file.write("Source: https://github.com/Official-Husko/GetFileList\n")
file.write("This report was generated at: " + dt + "\n")
file.write(seperator)
file.write("\n")
file.write("\n")
file.write("\n")
file.write(seperator)
file.write("General Information\n")
file.write(seperator)
file.write("Directory Name: " + os.path.basename(target) + "\n")
file.write("Directory Path: " + target + "\n")
file.write("File Count: " + str(len(files_list)) + "\n")
file.write("Directory Count: " + str(len(dirs_list)) + "\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write(seperator)
file.write("Present Directories\n")
file.write(seperator)
for folder in dirs_list:
file.write("[FOLDER] " + folder + "\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write(seperator)
file.write("Present Files\n")
file.write(seperator)
if args.calculate_hashes != "False":
for ffile, hashes in zip(files_list, hash_list):
file.write("[FILE] " + ffile + " " + str(hashes) + "\n")
else:
for ffile in files_list:
file.write("[FILE] " + ffile + "\n")
else:
if args.calculate_hashes != "False":
json_scheme = {
"info": {
"Disclaimer": "Generated using GetFileList by Husko ~ The right way to get & share a file list.",
"Source": "https://github.com/Official-Husko/GetFileList",
"This report was generated at": dt,
},
"General Information": {
"Directory Name": os.path.basename(target),
"Directory Path": target,
"File Count": str(len(files_list)),
"Directory Count": str(len(dirs_list))
},
"Present Directories": dirs_list,
"Present Files": {}
}
for ffile, hashes in zip(files_list, hash_list):
json_scheme["Present Files"][ffile] = hashes
else:
json_scheme = {
"info": {
"Disclaimer": "Generated using GetFileList by Husko ~ The right way to get & share a file list.",
"Source": "https://github.com/Official-Husko/GetFileList",
"This report was generated at": dt,
},
"General Information": {
"Directory Name": os.path.basename(target),
"Directory Path": target,
"File Count": str(len(files_list)),
"Directory Count": str(len(dirs_list))
},
"Present Directories": dirs_list,
"Present Files": files_list
}
with open("GetFileList Report - " + dtds + f".json", "w") as file:
json.dump(json_scheme, file, indent = 6)
print(colored("Done!", "green"))
sleep(3)
exit(0)