Skip to content

Instantly share code, notes, and snippets.

@torbiak
Created November 8, 2014 22:57
Show Gist options
  • Save torbiak/7f0b85d67add2aa57492 to your computer and use it in GitHub Desktop.
Save torbiak/7f0b85d67add2aa57492 to your computer and use it in GitHub Desktop.
find files based on their Windows "birthtime"
#!/usr/bin/python
# Recursively print files created before/at/after (-//+) some number of days
# ago. This only works on Windows, which stores file "birthtime" in the
# filesystem
import sys
import os
from datetime import date, timedelta
def main(args):
if args.days_ago.startswith('-'):
sign = -1
elif args.days_ago.startswith('+'):
sign = +1
else:
sign = 0
pivot = date.today() - timedelta(abs(int(args.days_ago)))
sep = "\n"
if args.null_delimit:
sep = chr(0)
for root, dirs, files in os.walk(args.dir):
for f in files:
path = os.path.join(root, f)
birthed = date.fromtimestamp(os.stat(path).st_birthtime)
if cmp(pivot, birthed) == sign:
sys.stdout.write(path)
sys.stdout.write(sep)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-0', dest='null_delimit',
action='store_true', help='delimit filepaths by nulls')
parser.add_argument('days_ago', type=str,
help="""\
Only print files created this many days ago. If preprended by +/-, only files
created after/before the number of days are printed. When using a minus sign
precede the arguments by -- so the argument parser doesn't treat it like an
option."""
)
parser.add_argument('dir', type=str, help='dir to start in')
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment