2020-08-20 17:58:17 +00:00
|
|
|
'''
|
|
|
|
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
|
|
|
|
|
2020-12-07 08:54:00 +00:00
|
|
|
from voussoirkit import interactive
|
2020-08-20 17:58:17 +00:00
|
|
|
from voussoirkit import pathclass
|
2020-08-20 18:29:10 +00:00
|
|
|
from voussoirkit import spinal
|
|
|
|
|
|
|
|
def prune_shortcuts(recurse=False, autoyes=False):
|
|
|
|
if recurse:
|
2021-05-18 00:00:51 +00:00
|
|
|
lnks = [file for file in spinal.walk('.') if file.extension == 'lnk']
|
2020-08-20 18:29:10 +00:00
|
|
|
else:
|
2020-09-22 08:56:17 +00:00
|
|
|
lnks = pathclass.cwd().glob('*.lnk')
|
2020-08-20 17:58:17 +00:00
|
|
|
|
2020-08-20 18:27:58 +00:00
|
|
|
stale = []
|
|
|
|
for lnk in lnks:
|
|
|
|
shortcut = winshell.Shortcut(lnk.absolute_path)
|
|
|
|
# There are some special shortcuts that do not have a path, but instead
|
|
|
|
# trigger some action based on a CLSID that Explorer understands.
|
|
|
|
# I can't find this information in the winshell.Shortcut object, so for
|
|
|
|
# now let's at least not delete these files.
|
|
|
|
if shortcut.path == '':
|
|
|
|
continue
|
|
|
|
if not os.path.exists(shortcut.path):
|
|
|
|
stale.append(lnk)
|
2020-08-20 17:58:17 +00:00
|
|
|
|
|
|
|
if not stale:
|
|
|
|
return
|
|
|
|
|
|
|
|
print(f'The following {len(stale)} will be recycled:')
|
|
|
|
for lnk in stale:
|
|
|
|
print(lnk.absolute_path)
|
|
|
|
print()
|
|
|
|
|
2020-12-07 08:54:00 +00:00
|
|
|
if autoyes or interactive.getpermission('Is that ok?'):
|
2020-08-20 17:58:17 +00:00
|
|
|
for lnk in stale:
|
|
|
|
print(lnk.absolute_path)
|
|
|
|
send2trash.send2trash(lnk.absolute_path)
|
|
|
|
|
|
|
|
def prune_shortcuts_argparse(args):
|
2020-08-20 18:29:10 +00:00
|
|
|
return prune_shortcuts(recurse=args.recurse, autoyes=args.autoyes)
|
2020-08-20 17:58:17 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
2021-02-21 05:01:55 +00:00
|
|
|
parser.add_argument('--recurse', action='store_true')
|
2020-08-20 17:58:17 +00:00
|
|
|
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:]))
|