Skip to content

Commit b7e5503

Browse files
authored
Merge pull request #3 from mjanowiecki/master
Update renameDirectories.py
2 parents 58bd4da + 017da39 commit b7e5503

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

renameDirectories.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
import os
22
import csv
33
from datetime import datetime
4+
import time
5+
import argparse
46

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()
612

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
728
f=csv.writer(open('renameLog'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb'))
829
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:
1234
reader = csv.DictReader(csvfile)
1335
for row in reader:
1436
oldFolder = row['oldFolder']
@@ -17,4 +39,12 @@
1739
oldPath = os.path.join(root,dir)
1840
newPath = os.path.join(root,newFolder)
1941
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

Comments
 (0)