|
1 | 1 | # Script Name : move_files_over_x_days.py |
2 | | -# Author : Craig Richards |
| 2 | +# Author(s) : Craig Richards ,Demetrios Bairaktaris |
3 | 3 | # Created : 8th December 2011 |
4 | | -# Last Modified : |
5 | | -# Version :1.0 |
6 | | -# Modifications : |
| 4 | +# Last Modified : 25 December 2017 |
| 5 | +# Version : 1.1 |
| 6 | +# Modifications : Added possibility to use command line arguments to specify source, destination, and days. |
7 | 7 | # Description : This will move all the files from the src directory that are over 240 days old to the destination directory. |
8 | 8 |
|
9 | 9 | import shutil |
10 | 10 | import sys |
11 | 11 | import time |
12 | 12 | import os |
13 | | -src = 'u:\\test' # Set the source directory |
14 | | -dst = 'c:\\test' # Set the destination directory |
| 13 | +import argparse |
| 14 | + |
| 15 | +usage = 'python move_files_over_x_days.py -src [SRC] -dst [DST] -days [DAYS]' |
| 16 | +description = 'Move files from src to dst if they are older than a certain number of days. Default is 240 days' |
| 17 | + |
| 18 | +args_parser = argparse.ArgumentParser(usage=usage, description=description) |
| 19 | +args_parser.add_argument('-src', '--src', type=str, nargs='?', default='.', help='(OPTIONAL) Directory where files will be moved from. Defaults to current directory') |
| 20 | +args_parser.add_argument('-dst', '--dst', type=str, nargs='?', required=True, help='(REQUIRED) Directory where files will be moved to.') |
| 21 | +args_parser.add_argument('-days', '--days', type=int, nargs='?', default=240, help='(OPTIONAL) Days value specifies the minimum age of files to be moved. Default is 240.') |
| 22 | +args = args_parser.parse_args() |
| 23 | + |
| 24 | +if args.days < 0: |
| 25 | + args.days = 0 |
| 26 | + |
| 27 | +src = args.src # Set the source directory |
| 28 | +dst = args.dst # Set the destination directory |
| 29 | +days = args.days #Set the number of days |
15 | 30 | now = time.time() # Get the current time |
| 31 | + |
| 32 | +if not os.path.exists(dst): |
| 33 | + os.mkdir(dst) |
| 34 | + |
16 | 35 | for f in os.listdir(src): # Loop through all the files in the source directory |
17 | | - if os.stat(f).st_mtime < now - 240 * 86400: # Work out how old they are, if they are older than 240 days old |
| 36 | + if os.stat(f).st_mtime < now - days * 86400: # Work out how old they are, if they are older than 240 days old |
18 | 37 | if os.path.isfile(f): # Check it's a file |
19 | 38 | shutil.move(f, dst) # Move the files |
20 | 39 |
|
0 commit comments