Please refer the below script to,Alert if more than 5 duplicate files in a folder.
This script will triggers whenever the given directory/folder which consists 5 or more than 5 duplicates files is there, then it will raises an alert.
NOTE: dict = findDup(‘C:\Users\Reebook12\Downloads’) ## you change your path here…
In the above code please, provide your path, then it will analyze the directory which consists the duplicates files or not.
# The script is a template to check UAC status on device.
import os
import sys
import _winreg
def alert(arg):
sys.stderr.write("%d%d%d" % (arg, arg, arg))
# Please use "alert(1)" to turn on the monitor(trigger an alert)
# Please use "alert(0)" to turn off the monitor(disable an alert)
# Please do not change above block and write your script below
## find and remove duplicate files, by hash (by file' content)!
import os
import hashlib
def hashfile(path, blocksize = 65536):
try:
afile = open(path, 'rb')
hasher = hashlib.md5()
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
afile.close()
return hasher.hexdigest()
except Exception as e:
print e
def findDup(parentFolder):
# Dups in format {hash:[names]}
dups = {}
for dirName, subdirs, fileList in os.walk(parentFolder):
print('Scanning %s...' % dirName)
for filename in fileList:
# Get the path to the file
path = os.path.join(dirName, filename)
# Calculate hash
file_hash = hashfile(path)
# Add or append the file path
if file_hash in dups:
dups[file_hash].append(path)
else:
dups[file_hash] = [path]
return dups
def printResults(dict1):
results = list(filter(lambda x: len(x) > 1, dict1.values()))
print len(results)
if len(results) >= 5:
print 'The directory you have selected CONSISTS FIVE(5)- OR- MORE THAN FIVE(5) DUPLICATE FILES'
alert(1)
else:
print('No,more than five(5) duplicate files found.')
alert(0)
def checkUAC():
if 'PROGRAMW6432' in os.environ.keys():
handle = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Policies\System", 0, _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY)
return (_winreg.QueryValueEx(handle, "EnableLUA")[0])
else:
key = getattr(_winreg, "HKEY_LOCAL_MACHINE")
subkey = _winreg.OpenKey(key, "Software\Microsoft\Windows\CurrentVersion\Policies\System")
return (_winreg.QueryValueEx(subkey, "EnableLUA")[0])
checkUAC()
if __name__ == '__main__':
dict = findDup('C:\\Users\\Reebook12\\Downloads') ## you change your path here...
printResults(dict)
SAMPLE OUTPUT:
20170609-dup5files.json (3.47 KB)