|
| 1 | +# The following code is written by Rohit Saxena. STUDY PURPOSE ONLY. Any unauthorized use or modification is prohibited. |
| 2 | +import ctypes |
| 3 | +import datetime |
| 4 | +import ftplib |
| 5 | +import glob |
| 6 | +import os |
| 7 | +import string |
| 8 | +from urllib.error import URLError |
| 9 | +from urllib.request import * |
| 10 | +import shutil |
| 11 | +import docx |
| 12 | +import openpyxl |
| 13 | + |
| 14 | +errorCount = 0 |
| 15 | +i = 0 |
| 16 | +j = 0 |
| 17 | +rvs = [] |
| 18 | +host = "" # FTP Host URL |
| 19 | +ftp_id = "" # FTP CLIENT ID |
| 20 | +ftp_pw = "" # FTP CLIENT PASSWORD |
| 21 | +_ftp_root_folder_ = 'htdocs' # FTP root folder |
| 22 | +newdir = os.environ['COMPUTERNAME'] # FTP Main folder name |
| 23 | +ft = [] |
| 24 | +todaysDate = str(datetime.datetime.today()) # FTP sub-folder name |
| 25 | +ftp = ftplib.FTP() |
| 26 | + |
| 27 | + |
| 28 | +def _rS_sign(): |
| 29 | + print("|""---------"" ""------------") |
| 30 | + print("|"" ""|"" "" |") |
| 31 | + print("|"" ""|"" "" |") |
| 32 | + print("|""---------"" ""|") |
| 33 | + print("|""-"" ""------------") |
| 34 | + print("|"" ""-"" ""|") |
| 35 | + print("|"" ""-"" ""|") |
| 36 | + print("|"" ""-"" "" ------------") |
| 37 | + print("The following code is written by Rohit Saxena. STUDY PURPOSE ONLY. Any unauthorized use or modification is " |
| 38 | + "prohibited.") |
| 39 | + return True |
| 40 | + |
| 41 | + |
| 42 | +def _rSAdmin_Check(): |
| 43 | + try: |
| 44 | + is_admin = (os.getuid() == 0) |
| 45 | + except AttributeError: |
| 46 | + is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0 |
| 47 | + return is_admin |
| 48 | + |
| 49 | + |
| 50 | +def _rSInternet_Check_(): # Internet Tester |
| 51 | + while True: |
| 52 | + try: |
| 53 | + urlopen('https://www.central16.in', timeout=1) |
| 54 | + return |
| 55 | + except URLError as e: |
| 56 | + print('URL Error: ', e.reason) # remove print is building exe |
| 57 | + pass |
| 58 | + |
| 59 | + |
| 60 | +def _RsBackupWriter(rrs, pp): # local backup (unfinished) |
| 61 | + global j |
| 62 | + print('BACKUP FOLDER STARTED /N rvs len : ', len(rrs)) |
| 63 | + trs = open(pp, "a") |
| 64 | + for item in rrs: |
| 65 | + trs.write("%s\n" % item) |
| 66 | + print('Backup File Created') |
| 67 | + file1 = open(pp, "r") |
| 68 | + print(file1.read()) |
| 69 | + file1.close() |
| 70 | + |
| 71 | + |
| 72 | +def _RsXlsx_(SS, CC): # XLSX Reader |
| 73 | + rs = openpyxl.load_workbook(CC, read_only=True, data_only=True) |
| 74 | + ws = rs.active |
| 75 | + for i in range(1, ws.max_row + 1): |
| 76 | + for j in range(1, ws.max_column + 1): |
| 77 | + if SS == ws.cell(i, j).value: |
| 78 | + print("Found in cell :", ws.cell(i, j), "File Location :", CC) |
| 79 | + rvs.append(CC) |
| 80 | + |
| 81 | + |
| 82 | +def _RsTxt_(SS, CC): # TXT Reader |
| 83 | + file1 = open(CC, 'r', encoding='UTF8', errors='ignore') |
| 84 | + flag = 0 |
| 85 | + index = 0 |
| 86 | + for line in file1: |
| 87 | + index += 1 |
| 88 | + if SS in line: |
| 89 | + flag = 1 |
| 90 | + break |
| 91 | + if flag != 0: |
| 92 | + print('String', SS, 'Found In Line: ', index, 'File Path: ', CC) |
| 93 | + rvs.append(CC) |
| 94 | + file1.close() |
| 95 | + |
| 96 | + |
| 97 | +def _RsDocx_(SS, CC): # DOCX Reader |
| 98 | + flag = 0 |
| 99 | + index = 0 |
| 100 | + doc = docx.Document(CC) |
| 101 | + for para in doc.paragraphs: |
| 102 | + index += 1 |
| 103 | + if SS in para.text: |
| 104 | + flag = 1 |
| 105 | + break |
| 106 | + if flag != 0: |
| 107 | + print('String', SS, 'Found In Line: ', index, 'File Path: ', CC) |
| 108 | + rvs.append(CC) |
| 109 | + |
| 110 | + |
| 111 | +def _RsDirCheck_(rsdc): # FTP Main Folder Checker |
| 112 | + x = 0 |
| 113 | + for g in rsdc: |
| 114 | + if newdir in g: |
| 115 | + x = 1 |
| 116 | + if x == 0: |
| 117 | + print(" New Directory created ---> ", newdir) |
| 118 | + ftp.mkd(newdir) |
| 119 | + |
| 120 | + |
| 121 | +def _RsTodayFolderCheck_(rstfc): # FTP Sub-Folder Checker |
| 122 | + x = 0 |
| 123 | + for g in rstfc: |
| 124 | + if todaysDate in g: |
| 125 | + x = 1 |
| 126 | + if x == 0: |
| 127 | + ftp.mkd(todaysDate) |
| 128 | + print("New Today's date folder created ---> ", todaysDate) |
| 129 | + |
| 130 | + |
| 131 | +def _Rs_ftp_COPY_(): # FTP Client connector |
| 132 | + print('uploading data...') |
| 133 | + port = 21 |
| 134 | + ftp.connect(host, port) |
| 135 | + print(ftp.getwelcome()) |
| 136 | + print("Logging in...") |
| 137 | + ftp.login(ftp_id, ftp_pw) |
| 138 | + ftp.cwd(_ftp_root_folder_) |
| 139 | + ftp.retrlines('LIST', ft.append) |
| 140 | + _RsDirCheck_(ft) |
| 141 | + ftp.cwd(newdir) |
| 142 | + ft.clear() |
| 143 | + ftp.retrlines('LIST', ft.append) |
| 144 | + _RsTodayFolderCheck_(ft) |
| 145 | + ftp.cwd(todaysDate) |
| 146 | + |
| 147 | + |
| 148 | +def _rS_FTP_file_transf(rvs): # FTP File uploader |
| 149 | + try: |
| 150 | + rs = 0 |
| 151 | + print('Uploading Data...') |
| 152 | + while rs != len(rvs): |
| 153 | + filename = rvs[rs] |
| 154 | + file_name, file_extension = os.path.splitext(filename) |
| 155 | + _FTP_File_Name_ = os.path.basename(file_name) + file_extension |
| 156 | + _ftpNewFileNMCmd_ = "STOR %s" % _FTP_File_Name_ |
| 157 | + with open(filename, "rb") as file: |
| 158 | + ftp.storbinary(_ftpNewFileNMCmd_, file) |
| 159 | + rs += 1 |
| 160 | + print('Upload Complete !') |
| 161 | + ftp.dir() |
| 162 | + except Exception: |
| 163 | + print(Exception) |
| 164 | + |
| 165 | + |
| 166 | +def main(): |
| 167 | + global errorCount |
| 168 | + if _rSAdmin_Check(): |
| 169 | + print("admin access") |
| 170 | + else: |
| 171 | + print("No admin access") |
| 172 | + global i, j |
| 173 | + global rvs |
| 174 | + # val = input("Enter extension : ") #If specific extension only required uncheck and add val in _Rel_Path |
| 175 | + ff = input("Enter string : ") |
| 176 | + rs = ['%s:\\' % d for d in string.ascii_uppercase if os.path.exists('%s:\\' % d)] |
| 177 | + print('Active local drives ---> ', rs) |
| 178 | + FtypeXLSX = '.xlsx' |
| 179 | + FtypeTXT = '.txt' |
| 180 | + FtypeDOCX = '.docx' |
| 181 | + _Rel_Exp_ = '**/*' |
| 182 | + _Rel_Path_ = _Rel_Exp_ |
| 183 | + # print('Selected file type = ', val) |
| 184 | + rss = [] |
| 185 | + |
| 186 | + bck_dir = "rrs" # LINE 154 - 166 REFERS LOCAL BACKUP (UNFINISHED) |
| 187 | + bck_file = "rrs.txt" |
| 188 | + print('Hidden folder named rss is created in first local drive from available drive list') |
| 189 | + pth = os.path.join(rs[0], bck_dir) |
| 190 | + if os.path.exists(pth): # this if will flush the old backup folder and re-create new backup folder |
| 191 | + shutil.rmtree(pth) |
| 192 | + os.makedirs(pth) |
| 193 | + print('Hidden folder path: ', pth) |
| 194 | + print('Hidden file text.txt is created in hidden folder rss') |
| 195 | + CN = os.path.join(pth, bck_file) |
| 196 | + print('Hidden file location: ', CN) |
| 197 | + file1 = open(CN, "a") |
| 198 | + file1.close() |
| 199 | + if _rS_sign(): |
| 200 | + print('Below files are present in system: '"\n") |
| 201 | + while i != len(rs): |
| 202 | + os.chdir(rs[i]) |
| 203 | + for file in glob.glob(_Rel_Path_, recursive=True): |
| 204 | + # print(file) |
| 205 | + completeName = os.path.join(rs[i], file) |
| 206 | + # print('File path: ', completeName) |
| 207 | + file_name, file_extension = os.path.splitext(completeName) |
| 208 | + if file_extension == FtypeXLSX: |
| 209 | + try: |
| 210 | + _RsXlsx_(ff, completeName) |
| 211 | + except Exception: |
| 212 | + errorCount += 1 |
| 213 | + print('Error in reading file, File Path: ', completeName) |
| 214 | + if file_extension == FtypeTXT: |
| 215 | + try: |
| 216 | + _RsTxt_(ff, completeName) |
| 217 | + except PermissionError: |
| 218 | + errorCount += 1 |
| 219 | + print('Admin Permission Required File Path: ', completeName) |
| 220 | + if file_extension == FtypeDOCX: |
| 221 | + try: |
| 222 | + _RsDocx_(ff, completeName) |
| 223 | + except Exception: |
| 224 | + errorCount += 1 |
| 225 | + print('Error in reading file, File Path: ', completeName) |
| 226 | + rss.append(completeName) |
| 227 | + i += 1 |
| 228 | + else: |
| 229 | + exit() |
| 230 | + print('System scan completed \n') |
| 231 | + # print(rss) |
| 232 | + print('Total File Scanned :', len(rss)) |
| 233 | + print("Total file found: ", len(rvs)) |
| 234 | + print("Total No of unreachable files (ACTION REQUIRED)", errorCount) |
| 235 | + if len(rvs) == 0: |
| 236 | + exit() |
| 237 | + print("\n", rvs) |
| 238 | + ''' |
| 239 | + _Rs_ftp_COPY_() |
| 240 | + _rS_FTP_file_transf(rvs) ONLY FOR FTP |
| 241 | + print('calling backup') |
| 242 | + _RsBackupWriter(rvs, CN) # LOCAL BACKUP CREATOR |
| 243 | + ''' |
| 244 | + # shutil.rmtree(pth) #FLUSH OLD BACKUP FILES (UNFINISHED) uncheck to delete local backup |
| 245 | + |
| 246 | + |
| 247 | +_rSInternet_Check_() |
| 248 | +if _rS_sign(): |
| 249 | + main() |
| 250 | +else: |
| 251 | + print("Author sign missing") |
0 commit comments