Skip to content

Script that checks for duplicate files and removes those the user selects #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added check_dup/.DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions check_dup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Application Opener
## Overview:

This script checks through a directory to see if there are any duplicates.

## Why would you find that useful?

It's useful so you don't need to open and close every file to see if you have any unnecessary files.


## How would you setup your own startup script?

1. As long as it is executable it should work (a quick "chmod +x" should do the trick)
2. The full path of the directory you're checking is an input, so keep that copied (everything else will be prompted to you)

142 changes: 142 additions & 0 deletions check_dup/check_dup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env python3

import os
import subprocess

# diff: 0 if same, 1 if different, 2 if trouble

# Global list
dontCheck = set()

def checkPrompt(fiFo, path):
# If duplicates found, notify that duplicates have been found
print()
print("Duplicates found for {}.".format(path))

# Display all the duplicates of that file in form (1: ... \n 2: ... etc.)
for key, fi in enumerate(fiFo):
print("{}. {}".format(key + 1, fi))

# Prompt if they would like to keep all files
keepAll = "X"
while keepAll != "y" and keepAll != "n":
keepAll = input("Would you like to keep all the above files? (y/n) ")

if keepAll == "n":
for fi in fiFo:
delFile = "X"
while delFile != "y" and delFile != "n":
delFile = input("Would you like to delete {}? (y/n)\t".format(fi))
if delFile == "y":
final = "X"
while final != "y" and final != "n":
final = input("(Safe Delete) Are you sure you would like to delete {}? (y/n)\t".format(fi))
if final == "y":
# Remove from list AND dir
#if os.path.exists(fi):
#os.remove(fi)
fiFo.remove(fi)

# Don't check these files again
for i in fiFo:
dontCheck.add(i)


def checkDup(checkDir, dirdir):
print()
print("Checking directory {}".format(checkDir))

# Walk through path and check if there are any duplicates
if dirdir:
# For dirpath, dirname, filenames in directory
for dp, dn, fn in os.walk(checkDir):
# For file in filenames
fiFound = []
for f in fn:
pathF = dp + "/" + f
pathF1 = pathF.replace(" ", "\\ ")
if os.path.isfile(pathF):
if pathF in dontCheck:
continue
for dirpath, dirnames, filenames in os.walk(checkDir):
for pp in filenames:
pathD = dirpath + "/" + pp
pathD1 = pathD.replace(" ", "\\ ")
cmd = "diff {} {}".format(pathF1,pathD1)
if os.path.isfile(pathD):
k = subprocess.call(cmd, shell=True, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
if k == 0:
fiFound.append(pathD)



if len(fiFound) > 1:
checkPrompt(fiFound, pathF)

else:
# Only the files directly in the directory
for files in os.listdir(checkDir):
fiFound = []
pathF = checkDir + "/" + files
pathF1 = pathF.replace(" ","\\ ")
if os.path.isfile(pathF):
if pathF in dontCheck:
continue
# Check files with themselves
for selif in os.listdir(checkDir):
pathD = checkDir + "/" + selif
pathD1 = pathD.replace(" ","\\ ")
cmd = "diff {} {}".format(pathF1,pathD1)
if os.path.isfile(pathD):
k = subprocess.call(cmd, shell=True, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
if k == 0:
fiFound.append(pathD)

if len(fiFound) > 1:
checkPrompt(fiFound, pathF)
print()


print("Duplicate Remover v1.0")
print("++ exit|stop|quit to stop script ++")
print()

x = "X"
while x != "y" and x != "n":
x = input("Would you like to check directories within directories? (y/n) ")
if x == "exit" or x == "stop" or x == "quit":
bye = "X"
while bye != "y" and bye != "n":
bye = input("Exit? (y/n) ")
if bye == "y":
print("Goodbye!")
exit()

if x == "y":
dirdir = True
else:
dirdir = False

print()

while True:
dontCheck.clear()
# Prompt inputs (ask which directory to search)
print("Pick a directory: ", end="")
checkDir = input()

# If exit, ask if they want to exit, else check dir
if checkDir == "exit" or checkDir == "stop" or checkDir == "quit":
bye = "X"
while bye != "y" and bye != "n":
bye = input("Exit? (y/n) ")
if bye == "y":
print("Goodbye!")
exit()

# Check if directory exists
if os.path.isdir(checkDir):
checkDup(checkDir, dirdir)
else:
print("Directory {} not found... (try pwd from the directory you want to check) \n".format(checkDir))
continue