Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions renameDirectories.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import os
import csv
from datetime import datetime
import time
import argparse

filePath = raw_input('Enter file path (C:/Test/): ')
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--directory', help='the file path of name changes. optional - if not provided, the script will ask for input')
parser.add_argument('-f', '--fileNameCSV', help='the CSV file of name changes. optional - if not provided, the script will ask for input')
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"')
args = parser.parse_args()

if args.directory:
directory = args.directory
else:
directory = raw_input('Enter the directory of the files to be renamed: ')
if args.fileNameCSV:
fileNameCSV = args.fileNameCSV
else:
fileNameCSV = raw_input('Enter the CSV file of name changes (including \'.csv\'): ')
if args.makeChanges:
makeChanges = args.makeChanges
else:
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): ')

startTime = time.time()
print startTime
f=csv.writer(open('renameLog'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb'))
f.writerow(['oldLocation']+['newLocation'])
for root, dirs, files in os.walk(filePath, topdown=True):
for dir in dirs:
with open('FolderNames.csv') as csvfile:
for root, dirs, files in os.walk(directory, topdown=False):
for dir in dirs :
print dir
with open(fileNameCSV) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
oldFolder = row['oldFolder']
Expand All @@ -17,4 +39,12 @@
oldPath = os.path.join(root,dir)
newPath = os.path.join(root,newFolder)
f.writerow([oldPath]+[newPath])
os.rename(oldPath,newPath)
if makeChanges == 'true':
os.rename(oldPath,newPath)
else:
print 'log of expected directory name changes created only, no files renamed'

elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s)