From cd27eff4d586737971a6030072aa57c5bf2b9cff Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Fri, 9 Nov 2012 17:09:47 -0500 Subject: [PATCH] Added basic license and some useful snippets. --- empty_file_finder.py | 39 ++++++++++++++++++++++++++++++ mass_rename_walk.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ search_for_ext.py | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 empty_file_finder.py create mode 100644 mass_rename_walk.py create mode 100644 search_for_ext.py diff --git a/empty_file_finder.py b/empty_file_finder.py new file mode 100644 index 0000000..241b580 --- /dev/null +++ b/empty_file_finder.py @@ -0,0 +1,39 @@ +#! /usr/bin/env python + +import re, os, sys, stat + +def main(): + + 'Check if there are enough arguments supplied.' + if check_args(): + + 'Keep the current directory.' + curr_dir = os.getcwd() + + 'Walk the directories, find matches and rename them' + for root, dirs, files in os.walk(curr_dir): + + for name in files: + path = os.path.join(root, name) + + if(os.path.isfile(path)): + if(os.path.getsize(path) == 0): + print(path + " found.") + + +def check_args(): + '''Checks if there are enough arguments to go on.''' + + arg_num = len(sys.argv) + if arg_num < 1: + 'Print out a pretty usage statement.' + print() + print("Usage:") + print("python empty_file_finder.py") + + return False + else: + return True + + +if __name__ == "__main__": main() diff --git a/mass_rename_walk.py b/mass_rename_walk.py new file mode 100644 index 0000000..b7d13e4 --- /dev/null +++ b/mass_rename_walk.py @@ -0,0 +1,56 @@ +#! /usr/bin/env python +'''A recursive, and mass rename utility. Especially useful for changing +extensions on files.''' + +import re, os, sys + +global old_ext, new_ext + +def main(): + print("Massive Extension Renaming Tool") + print("-------------------------------") + print("Code: Dorian Pula") + + 'Check if there are enough arguments supplied.' + if check_args(): + 'Grab the arguments.' + old_ext = sys.argv[1] + new_ext = sys.argv[2] + + 'Keep the current directory.' + curr_dir = os.getcwd() + + 'Set up the regex matching.' + matcher = re.compile("^.+\." + old_ext + "$") + + 'Walk the directories, find matches and rename them' + for root, dirs, files in os.walk(curr_dir): + print("Root dir: " + root) + + for name in files: + if matcher.match(name): + path, ext = os.path.splitext(os.path.join(root, name)) + new_path = path + "." + new_ext + os.rename(os.path.join(root, name), new_path) + print(os.path.join(root, name) + " to " + new_path) + + +def check_args(): + '''Checks if there are enough arguments to go on.''' + + arg_num = len(sys.argv) + if arg_num < 3: + 'Print out a pretty usage statement.' + print() + print("Usage:") + print("python mass_rename_walk.py ") + print(" : Rename files with this extension.") + print(" : Rename files to this extension.") + print("NOTE: DO NOT INCLUDE THE . IN THE EXTENSIONS!") + + return False + else: + return True + + +if __name__ == "__main__": main() diff --git a/search_for_ext.py b/search_for_ext.py new file mode 100644 index 0000000..e9a5484 --- /dev/null +++ b/search_for_ext.py @@ -0,0 +1,49 @@ +#! /usr/bin/env python +'''A recursive, removal utility that focuses on files with a target extension.''' + +import re, os, sys + +def main(): + print("Search by Extension Tool") + print("-------------------------------") + print("Code: Dorian 'deepwave' Pula") + + 'Check if there are enough arguments supplied.' + if check_args(): + 'Grab the arguments.' + trg_ext = sys.argv[1] + + 'Keep the current directory.' + curr_dir = os.getcwd() + + 'Set up the regex matching.' + matcher = re.compile("^.+\." + trg_ext + "$") + + 'Walk the directories, find matches and rename them' + for root, dirs, files in os.walk(curr_dir): + print("Searching in: " + root) + + for name in files: + if matcher.match(name): + path = os.path.join(root, name) + print(path + " found.") + + +def check_args(): + '''Checks if there are enough arguments to go on.''' + + arg_num = len(sys.argv) + if arg_num < 2: + 'Print out a pretty usage statement.' + print() + print("Usage:") + print("python search_by_ext.py ") + print(" : Find files with this extension.") + print("NOTE: DO NOT INCLUDE THE . IN THE EXTENSIONS!") + + return False + else: + return True + + +if __name__ == "__main__": main()