Skip to content

Commit d10398c

Browse files
committed
updates
1 parent 1305ad6 commit d10398c

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Based on the file path entered and file extension specified, extracts the names
1313
#### [extractFolderNames.py](/extractFilenames.py)
1414
Based on the file path entered, extracts the names of all nested folders and writes them to a CSV file.
1515

16+
#### [generateChecksumsForFilesInDirectory.py](/generateChecksumsForFilesInDirectory.py)
17+
Based on the file path entered, creates a CSV of all files in that directory and the files' MD5 checksums.
18+
1619
#### [renameDirectories.py](/renameDirectories.py)
1720
Based on the file path entered, renames all nested folders according to a CSV file named 'FolderNames.csv' with the columns named 'oldFolder' and 'newFolder,' provided that there is a match between the nested folders and the names in the 'oldFolder' column. The script writes a log of all folders that were renamed, including their old name and their new name.
1821

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import hashlib
2+
import csv
3+
import argparse
4+
import os
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('-d', '--directory', help='the directory of the files. optional - if not provided, the script will ask for input')
8+
args = parser.parse_args()
9+
10+
if args.directory:
11+
directory = args.directory
12+
else:
13+
directory = raw_input('Enter directory (C:/Test/): ')
14+
15+
fileList = []
16+
for root, dirs, files in os.walk(directory, topdown=True):
17+
for file in files:
18+
fileList.append(os.path.join(root, file).replace('\\','/'))
19+
20+
f=csv.writer(open('fileChecksums.csv', 'wb'))
21+
f.writerow(['fileName']+['checksum'])
22+
23+
for file in fileList:
24+
fileName = file[file.rindex('/')+1:]
25+
print fileName
26+
file = open(file,'rb').read()
27+
checksum = hashlib.md5(file).hexdigest()
28+
f.writerow([fileName]+[checksum])

renameFiles.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66

77
parser = argparse.ArgumentParser()
88
parser.add_argument('-d', '--directory', help='the directory of the files to be renamed. optional - if not provided, the script will ask for input')
9-
parser.add_argument('-f', '--fileName', help='the CSV file 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')
1010
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"')
1111
args = parser.parse_args()
1212

1313
if args.directory:
1414
directory = args.directory
1515
else:
1616
directory = raw_input('Enter the directory of the files to be renamed: ')
17-
if args.fileName:
18-
fileName = args.fileName
17+
if args.fileNameCSV:
18+
fileNameCSV = args.fileNameCSV
1919
else:
20-
fileName = raw_input('Enter the CSV file of name changes (including \'.csv\'): ')
20+
fileNameCSV = raw_input('Enter the CSV file of name changes (including \'.csv\'): ')
2121
if args.makeChanges:
2222
makeChanges = args.makeChanges
2323
else:
@@ -28,7 +28,7 @@
2828
f.writerow(['oldFilename']+['newFilename'])
2929
for root, dirs, files in os.walk(directory, topdown=True):
3030
for file in files:
31-
with open(nameChangeFile) as csvfile:
31+
with open(fileNameCSV) as csvfile:
3232
reader = csv.DictReader(csvfile)
3333
for row in reader:
3434
oldFilename = row['file']
@@ -38,7 +38,7 @@
3838
oldPath = os.path.join(root,file)
3939
newPath = os.path.join(root,newFilename)
4040
f.writerow([oldPath]+[newPath])
41-
if makeChanges == 'true'
41+
if makeChanges == 'true':
4242
os.rename(oldPath,newPath)
4343
else:
4444
print 'log of expected file name changes created only, no files renamed'

0 commit comments

Comments
 (0)