代碼類型:python 功能說明:獲取文件MD5,及最後修改内容時間 import subprocess import time import os class getmd5(object): def get_MD5(self, fun,batpath,logpath): print("正在獲取MD5,請稍後...") print("開始遍歷目錄下的bat程式") list = os.listdir(batpath) with open("%s\%s.txt" % (logpath, fun), "a")as f: print("開始計算程式MD5....") for i in list: if i.split(".")[-1] == "bat" or i.endswith(".BAT") or i.endswith(".cmd") or i.endswith(".ini"): batname = os.path.join(batpath, i) lasttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.stat(batname).st_mtime)) pp = 'Certutil -hashfile %s MD5 | find /i /v "MD5" |find /i /v "Certutil"' % batname mm = subprocess.getoutput("%s" % pp) f.write(batname + "," + mm + "," + lasttime + "\n") print("MD5計算完成") f.close()
-------------------------- 此处为getmd5 的总程式 --------------------------
import os import sys from chkmd5 import check from getmd5 import getmd5 class path(object): def __init__(self): self.batpath = r"E:\old\F1ABBA" self.logpath = r"E:\old\temp" def mklog(self,i): # 刪除log文件夾 if not os.path.exists(self.logpath): os.mkdir(self.logpath) # shutil.rmtree(self.logpath) log = os.path.join(self.logpath, i)+".txt" if os.path.exists(log): os.remove(log) if i == "getmd5": getmd5().get_MD5("getmd5",self.batpath,self.logpath) print("get md5 成功!") elif i=="chkmd5": check().check_Md5("chkmd5",self.batpath,self.logpath) print("check md5 成功!") else: self.run() def run(self,argv): self.mklog(argv[0]) if __name__ == '__main__': run=path() run.run(sys.argv[1:])