Skip to content

Commit 1433c38

Browse files
authored
Merge pull request #3 from ehanson8/master
Updating scripts
2 parents 459f940 + dda73b5 commit 1433c38

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
Scripts for moving files and renaming directories
33

44
#### [collapseDirectories.py](/collapseDirectories.py)
5-
Based on the file path entered, this script moves any file existing below the eighth level folder in a Linux directory structure (i.e. /media/bitCurator/Storage/Collection/IncomingTransfer/BatchNumber/Disc/JobName) up to the eigth level folder. The script writes a log of all files that were moved, including their old location and their new location.
5+
Based on the file path entered, moves any file existing below the eighth level folder in a Linux directory structure (i.e. /media/bitCurator/Storage/Collection/IncomingTransfer/BatchNumber/Disc/JobName) up to the eigth level folder. The script writes a log of all files that were moved, including their old location and their new location.
66

7-
#### [extractFolderNames.py](/extractFilenames.py)
8-
Based on the file path entered, this script extracts the names of all nested folders and writes them to a CSV file.
7+
#### [compareFilesInTwoDirectories.py](/compareFilesInTwoDirectories.py.py)
8+
Based on the primary and secondary file paths entered, produces two CSV files that list the files that exist in one directory but are missing from other directory.
99

1010
#### [extractFilenames.py](/extractFilenames.py)
11-
Based on the file path entered and file extension specified, this script extracts the names of all relevant files in the directory and writes them to a CSV file. Additionally, you can specify a prefix to be added to the edited file name in the "newFile" column. Additionally, this script creates a log of all files, whether they have the specified extension or not, in the directory for troubleshooting potential problems.
11+
Based on the file path entered and file extension specified, extracts the names of all relevant files in the directory and writes them to a CSV file. Additionally, you can specify a prefix to be added to the edited file name in the "newFile" column. Additionally, this script creates a log of all files, whether they have the specified extension or not, in the directory for troubleshooting potential problems.
1212

13-
#### [renameFiles.py](/renameFiles.py)
14-
Based on the file path entered, this script renames files in the directory according to a specified CSV file with the columns named 'file' and 'newFile,' provided that there is a match between the files and the names in the 'file' column. The script writes a log of all files that were renamed, including their old name and their new name.
13+
#### [extractFolderNames.py](/extractFilenames.py)
14+
Based on the file path entered, extracts the names of all nested folders and writes them to a CSV file.
1515

1616
#### [renameDirectories.py](/renameDirectories.py)
17-
Based on the file path entered, this script 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.
17+
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.
18+
19+
#### [renameFiles.py](/renameFiles.py)
20+
Based on the file path entered, renames files in the directory according to a specified CSV file with the columns named 'file' and 'newFile,' provided that there is a match between the files and the names in the 'file' column. The script writes a log of all files that were renamed, including their old name and their new name. The user will be prompted as to whether they want the file name changes executed or whether they just want log of the expected file name changes.
1821

1922
#### [samplingScript.py](/samplingScript.py)
20-
Based on the file path entered, the script selects every 10th file of a folder (after skipping the first file) and moves it over identically named folder in a 'sampled' directory while the skipped files are moved into an identically named folder in an 'unsampled' directory. The script also writes 'sampled' and 'unsampled' logs of all files that were moved, including their old location and their new location.
23+
Based on the file path entered, selects every 10th file of a folder (after skipping the first file) and moves it over identically named folder in a 'sampled' directory while the skipped files are moved into an identically named folder in an 'unsampled' directory. The script also writes 'sampled' and 'unsampled' logs of all files that were moved, including their old location and their new location.

compareFilesInTwoDirectories.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import csv
3+
from datetime import datetime
4+
import time
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('-1', '--primaryFilePath', help='the primary file path (C:/Test/). optional - if not provided, the script will ask for input')
8+
parser.add_argument('-2', '--secondaryFilePath', help='the secondary file path (C:/Test/). optional - if not provided, the script will ask for input')
9+
args = parser.parse_args()
10+
11+
if args.primaryFilePath:
12+
key = args.primaryFilePath
13+
else:
14+
primaryFilePath = raw_input('Enter primary file path (C:/Test/): ')
15+
if args.secondaryFilePath:
16+
key2 = args.secondaryFilePath
17+
else:
18+
secondaryFilePath = raw_input('Enter secondary file path (C:/Test/): ')
19+
20+
startTime = time.time()
21+
f=csv.writer(open('filesMissingFromSecondaryDirectory'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb'))
22+
f.writerow(['directory']+['file'])
23+
f2=csv.writer(open('filesMissingFromPrimaryDirectory'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb'))
24+
f2.writerow(['directory']+['file'])
25+
primaryFilePathDict = {}
26+
for root, dirs, files in os.walk(primaryFilePath, topdown=True):
27+
for file in files:
28+
primaryFilePathDict[file] = root
29+
secondaryFilePathDict = {}
30+
for root, dirs, files in os.walk(secondaryFilePath, topdown=True):
31+
for file in files:
32+
secondaryFilePathDict[file] = root
33+
34+
for k, v in primaryFilePathDict.items():
35+
if k in secondaryFilePathDict:
36+
pass
37+
else:
38+
f.writerow([v]+[k])
39+
40+
for k, v in secondaryFilePathDict.items():
41+
if k in primaryFilePathDict:
42+
pass
43+
else:
44+
f2.writerow([v]+[k])
45+
46+
elapsedTime = time.time() - startTime
47+
m, s = divmod(elapsedTime, 60)
48+
h, m = divmod(m, 60)
49+
print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s)

extractFilenames.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
fileExtension = raw_input('Enter file extension: ')
77
prefix = raw_input('Enter prefix to be added: ')
88

9+
startTime = time.time()
910
f = csv.writer(open('fileListing.csv', 'wb'))
1011
f.writerow(['file'] + ['newFile'])
1112
f2 = csv.writer(open('fullfileListing.csv', 'wb'))
@@ -14,7 +15,7 @@
1415
for root, dirs, files in directories:
1516
for file in files:
1617
f2.writerow([file])
17-
if file[-3:] == fileExtension
18+
if file[-3:] == fileExtension:
1819
itemID = file[0:5]
1920
newFile = prefix + itemID + fileExtension
2021
f.writerow([file] + [newFile])

renameFiles.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22
import csv
33
from datetime import datetime
44
import time
5+
import argparse
56

6-
filePath = raw_input('Enter file path (C:/Test/): ')
7-
nameChangeFile = raw_input('Enter name of CSV with name changes: ')
7+
parser = argparse.ArgumentParser()
8+
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')
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()
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.fileName:
18+
fileName = args.fileName
19+
else:
20+
fileName = 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 files (otherwise, it will only create a log of the expected file name changes): ')
825

926
startTime = time.time()
1027
f=csv.writer(open('renameLog'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb'))
1128
f.writerow(['oldFilename']+['newFilename'])
12-
for root, dirs, files in os.walk(filePath, topdown=True):
29+
for root, dirs, files in os.walk(directory, topdown=True):
1330
for file in files:
1431
with open(nameChangeFile) as csvfile:
1532
reader = csv.DictReader(csvfile)
@@ -21,8 +38,10 @@
2138
oldPath = os.path.join(root,file)
2239
newPath = os.path.join(root,newFilename)
2340
f.writerow([oldPath]+[newPath])
24-
#Uncomment the following line to acutally rename files rather than just writing a log file of the changes to be made
25-
#os.rename(oldPath,newPath)
41+
if makeChanges == 'true'
42+
os.rename(oldPath,newPath)
43+
else:
44+
print 'log of expected file name changes created only, no files renamed'
2645

2746
elapsedTime = time.time() - startTime
2847
m, s = divmod(elapsedTime, 60)

0 commit comments

Comments
 (0)