Phillip Alday avatar Phillip Alday committed ef7bc69

Added ability to invert selection from extension.

Comments (0)

Files changed (1)

 }
 
 def main(argv=None):
-    parser = argparse.ArgumentParser()
+    parser = argparse.ArgumentParser(
+                        description="A utility for finding and dealing with duplicate files",
+                        epilog = "How much disk space can you save?")
     parser.add_argument('--size-only', metavar="SIZE", type=str,
                         default=str(sys.maxint),
                         help="Only use size comparison on files "
                         help="paths to search")
     parser.add_argument('-e','--extension', type=str, default=None, nargs='+',
                         help="Limit search to files of a certain extension.")
+    parser.add_argument('--invert', action="store_true",default=False,
+                        help="Invert selection of extensions, i.e. negative match.")
 
     args = parser.parse_args(argv)
     args.final_byte_check = False
     args.size_only, args.max_size, args.min_size = map(size_to_int, [args.size_only, args.max_size, args.min_size])
     find_duplicates(args.path, args)
 
-def find_files(args, ext=None):
+def find_files(args, ext=None, invert=False):
     """Find all files in the search path optionally matching the extension.
 
     Keyword arguments:
         if os.path.isdir(ddir):
             for root, dirs, fnames in os.walk(ddir):
                 for f in fnames:
-                    if ext is None or os.path.splitext(f)[1] in ext:
+                    if ext is None or ((os.path.splitext(f)[1] in ext) != invert):
                             yield os.path.join(root, f)
         else:
-            if ext is None or os.path.splitext(f)[1] in ext:
+            if ext is None or ((os.path.splitext(f)[1] in ext) != invert):
                     yield ddir
 
 def group_pairs(pairs):
                 -- prompt_for_action -- prompt for action on each set of
                                         duplicates logical mutually exclusive
                                         with summary_only
-                -- extension         -- file name extension to restrict search
+                -- extension         -- file name extensions to restrict search
+                -- invert            -- negative matching for file name extension
                 -- max_size          -- ignore files larger than this size
                 -- min_size          -- ignore files larger than this size
                 -- size_only         -- comparisons only on size for files
 
     # traverse the directory tree, count the files and get their size
     # this gets sizes for all files, even files in the ignore range
-    for fn in find_files(dirs, opts.extension):
+    for fn in find_files(dirs, opts.extension, opts.invert):
         if not os.path.isfile(fn):
             continue
         count += 1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.