From 90c8643ef360ad8eef841e405d1128a2bd0ebe10 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 20 Aug 2020 10:58:17 -0700 Subject: [PATCH] Add prune_shortcuts.py. --- prune_shortcuts.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 prune_shortcuts.py diff --git a/prune_shortcuts.py b/prune_shortcuts.py new file mode 100644 index 0000000..d81eb33 --- /dev/null +++ b/prune_shortcuts.py @@ -0,0 +1,44 @@ +''' +This program deletes windows .lnk files if the path they point to no longer +exists. +''' +import argparse +import os +import send2trash +import sys +import winshell + +from voussoirkit import getpermission +from voussoirkit import pathclass + +def prune_shortcuts(autoyes=False): + lnks = pathclass.Path('.').glob('*.lnk') + stale = [lnk for lnk in lnks if not os.path.exists(winshell.Shortcut(lnk.absolute_path).path)] + + if not stale: + return + + print(f'The following {len(stale)} will be recycled:') + for lnk in stale: + print(lnk.absolute_path) + print() + + if autoyes or getpermission.getpermission('Is that ok?'): + for lnk in stale: + print(lnk.absolute_path) + send2trash.send2trash(lnk.absolute_path) + +def prune_shortcuts_argparse(args): + return prune_shortcuts(autoyes=args.autoyes) + +def main(argv): + parser = argparse.ArgumentParser(description=__doc__) + + parser.add_argument('--yes', dest='autoyes', action='store_true') + parser.set_defaults(func=prune_shortcuts_argparse) + + args = parser.parse_args(argv) + return args.func(args) + +if __name__ == '__main__': + raise SystemExit(main(sys.argv[1:]))