''' Created on Sep 19, 2015 @author: Brett Paufler Copyright Brett Paufler Catalogs the Computer Saves a Listing of all: Files & File Stats file_path, file_stats file_path, file_stats Directories dir_path dir_path saved as two separate text.txt files 'C:\ TODO data_computer\ %s_computer_file_listing.csv' % time_now As with other large data files (reddit scans), this is saved outside of the eclipse workspace if any errors reading any of the files, a third text file is saved #TODO:This is intended to be the First Part of a System Checker Anti-Virus, Sanity Check, Whatever #TODO: Probably should limit to C:\\ ONLY #TODO: directory stats, count, comp_compare, size, average, max, min, directory stats ''' from os import walk, stat from os.path import join #from csv import writer from datetime import datetime from comp_common import data_dir, time_format_string def catalog_computer(base_dir='C:\\'): '''Saves list of all files and directores as .txt file file_path, file_stats If there is a problem reading any file (too long, UTF encoding, not present): Error Report is generated. Mostly, these files should be deleted from system or the path renamed. base_dir should be a valid path: C:\ TODO\\''' print 'CATALOGUE COMMENCING' #init the holding variables file_text = '"path", st_mode, st_ino, st_dev, st_nlink, st_uid, ' file_text += 'st_gid, st_size, st_atime, st_mtime, st_ctime\n' dir_text = '' error_text = '' #main scan loop for root, _, names in walk(base_dir): print 'SCANNING DIR: %s' % root dir_text += root + '\n' for name in names: f = join(root, name) try: file_text += ','.join(['"%s"' % f] + [str(a) for a in stat(f)]) + '\n' except WindowsError: print 'ERROR READING: %s' % f error_text += f + '\n' #save name logic if base_dir == 'C:\\': p = 'computer' else: p = base_dir.replace('\\', '-') p = p.replace(':', '') time_now = datetime.now().strftime(time_format_string) fsN = '%s_%s_file_listing.txt' % (time_now, p) dsN = '%s_%s_directories.txt' % (time_now, p) esN = '%s_%s_errors.txt' % (time_now, p) #Write data to disk #dir_out = 'C:\ TODO' with open(join(data_dir, fsN), 'wb') as file_out: file_out.write(file_text) with open(join(data_dir, dsN), 'wb') as file_out: file_out.write(dir_text) if error_text: with open(join(data_dir, esN), 'wb') as file_out: file_out.write(error_text) print '\n\nERRORS:' print error_text print 'END ERRORS' print 'CATALOGUE COMPLETE' if __name__ == '__main__': base_dir='C:\\' #base_dir = 'path' #\data_computer' catalog_computer(base_dir)