|
1 | 1 | import os |
2 | 2 | import csv |
3 | 3 | from datetime import datetime |
| 4 | +import time |
| 5 | +import argparse |
4 | 6 |
|
5 | | -filePath = raw_input('Enter file path (C:/Test/): ') |
| 7 | +parser = argparse.ArgumentParser() |
| 8 | +parser.add_argument('-d', '--directory', help='the file path of name changes. optional - if not provided, the script will ask for input') |
| 9 | +parser.add_argument('-f', '--fileNameCSV', help='the CSV file of name changes. optional - if not provided, the script will ask for input') |
| 10 | +parser.add_argument('-m', '--makeChanges', help='Enter "true" to if the script should actually rename the files (otherwise, it will only create a log of the expected file name changes). optional - if not provided, the script will to "false"') |
| 11 | +args = parser.parse_args() |
6 | 12 |
|
| 13 | +if args.directory: |
| 14 | + directory = args.directory |
| 15 | +else: |
| 16 | + directory = raw_input('Enter the directory of the files to be renamed: ') |
| 17 | +if args.fileNameCSV: |
| 18 | + fileNameCSV = args.fileNameCSV |
| 19 | +else: |
| 20 | + fileNameCSV = raw_input('Enter the CSV file of name changes (including \'.csv\'): ') |
| 21 | +if args.makeChanges: |
| 22 | + makeChanges = args.makeChanges |
| 23 | +else: |
| 24 | + makeChanges = raw_input('Enter "true" to if the script should actually rename the directories (otherwise, it will only create a log of the expected directory name changes): ') |
| 25 | + |
| 26 | +startTime = time.time() |
| 27 | +print startTime |
7 | 28 | f=csv.writer(open('renameLog'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb')) |
8 | 29 | f.writerow(['oldLocation']+['newLocation']) |
9 | | -for root, dirs, files in os.walk(filePath, topdown=True): |
10 | | - for dir in dirs: |
11 | | - with open('FolderNames.csv') as csvfile: |
| 30 | +for root, dirs, files in os.walk(directory, topdown=False): |
| 31 | + for dir in dirs : |
| 32 | + print dir |
| 33 | + with open(fileNameCSV) as csvfile: |
12 | 34 | reader = csv.DictReader(csvfile) |
13 | 35 | for row in reader: |
14 | 36 | oldFolder = row['oldFolder'] |
|
17 | 39 | oldPath = os.path.join(root,dir) |
18 | 40 | newPath = os.path.join(root,newFolder) |
19 | 41 | f.writerow([oldPath]+[newPath]) |
20 | | - os.rename(oldPath,newPath) |
| 42 | + if makeChanges == 'true': |
| 43 | + os.rename(oldPath,newPath) |
| 44 | + else: |
| 45 | + print 'log of expected directory name changes created only, no files renamed' |
| 46 | + |
| 47 | +elapsedTime = time.time() - startTime |
| 48 | +m, s = divmod(elapsedTime, 60) |
| 49 | +h, m = divmod(m, 60) |
| 50 | +print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s) |
0 commit comments