Skip to content

Commit 6d91f3b

Browse files
Merge pull request geekcomputers#279 from DemetriosBairaktaris/master
Add option to use command line arguments to script
2 parents 15c128e + 9dd8257 commit 6d91f3b

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

move_files_over_x_days.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
# Script Name : move_files_over_x_days.py
2-
# Author : Craig Richards
2+
# Author(s) : Craig Richards ,Demetrios Bairaktaris
33
# 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.
77
# Description : This will move all the files from the src directory that are over 240 days old to the destination directory.
88

99
import shutil
1010
import sys
1111
import time
1212
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
1530
now = time.time() # Get the current time
31+
32+
if not os.path.exists(dst):
33+
os.mkdir(dst)
34+
1635
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
1837
if os.path.isfile(f): # Check it's a file
1938
shutil.move(f, dst) # Move the files
2039

0 commit comments

Comments
 (0)