Last active
February 21, 2019 23:59
-
-
Save alexander-hanel/fb6cd8b2b2c3ba270a1d3426a11e35b1 to your computer and use it in GitHub Desktop.
name to compile time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import glob | |
import hashlib | |
import os | |
import pefile | |
import sys | |
def rename_timestamp(file_path): | |
try: | |
data = open(file_path, "rb").read() | |
temp_time = 0 | |
try: | |
pe = pefile.PE(data=data) | |
temp_time = str(datetime.datetime.fromtimestamp(pe.FILE_HEADER.TimeDateStamp)).replace(" ", "-") | |
except: | |
pass | |
file_hash = hashlib.sha256(data).hexdigest() | |
if temp_time == 0: | |
file_name = "invalid-%s" % (file_hash) | |
else: | |
file_name = "%s-%s" % (temp_time, file_hash) | |
dir_name = os.path.dirname(file_path) | |
os.rename(os.path.join(dir_name, file_path), os.path.join(dir_name, file_name)) | |
except Exception as e: | |
print e | |
return | |
if len(sys.argv) < 2: | |
for file_path in glob.glob("*"): | |
rename_timestamp(file_path) | |
else: | |
rename_timestamp(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment