Skip to content

Commit 459f940

Browse files
author
Elizabeth England
committed
2 parents 675c704 + a3c924e commit 459f940

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ $RECYCLE.BIN/
4545
Network Trash Folder
4646
Temporary Items
4747
.apdisk
48+
*.csv

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
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. The log is written in the command line utility's home directory.
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.
66

7-
#### [extractFolderNames.py](/extractFolderNames.py)
7+
#### [extractFolderNames.py](/extractFilenames.py)
88
Based on the file path entered, this script extracts the names of all nested folders and writes them to a CSV file.
99

10+
#### [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.
12+
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.
15+
1016
#### [renameDirectories.py](/renameDirectories.py)
11-
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. The log is written in the command line utility's home directory.
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.
1218

1319
#### [samplingScript.py](/samplingScript.py)
14-
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. The logs are written in the command line utility's home directory.
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.

extractFilenames.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import csv
2+
import os
3+
import time
4+
5+
filePath = raw_input('Enter file path (C:/Test/): ')
6+
fileExtension = raw_input('Enter file extension: ')
7+
prefix = raw_input('Enter prefix to be added: ')
8+
9+
f = csv.writer(open('fileListing.csv', 'wb'))
10+
f.writerow(['file'] + ['newFile'])
11+
f2 = csv.writer(open('fullfileListing.csv', 'wb'))
12+
f2.writerow(['file'] + ['newFile'])
13+
directories = os.walk(filePath, topdown=True)
14+
for root, dirs, files in directories:
15+
for file in files:
16+
f2.writerow([file])
17+
if file[-3:] == fileExtension
18+
itemID = file[0:5]
19+
newFile = prefix + itemID + fileExtension
20+
f.writerow([file] + [newFile])
21+
22+
elapsedTime = time.time() - startTime
23+
m, s = divmod(elapsedTime, 60)
24+
h, m = divmod(m, 60)
25+
print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s)

renameFiles.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import csv
3+
from datetime import datetime
4+
import time
5+
6+
filePath = raw_input('Enter file path (C:/Test/): ')
7+
nameChangeFile = raw_input('Enter name of CSV with name changes: ')
8+
9+
startTime = time.time()
10+
f=csv.writer(open('renameLog'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb'))
11+
f.writerow(['oldFilename']+['newFilename'])
12+
for root, dirs, files in os.walk(filePath, topdown=True):
13+
for file in files:
14+
with open(nameChangeFile) as csvfile:
15+
reader = csv.DictReader(csvfile)
16+
for row in reader:
17+
oldFilename = row['file']
18+
newFilename = row['newFile']
19+
if file == oldFilename:
20+
print oldFilename
21+
oldPath = os.path.join(root,file)
22+
newPath = os.path.join(root,newFilename)
23+
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)
26+
27+
elapsedTime = time.time() - startTime
28+
m, s = divmod(elapsedTime, 60)
29+
h, m = divmod(m, 60)
30+
print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s)

samplingScript.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
f2=csv.writer(open('unsampledLog'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv','wb'))
1212
f2.writerow(['oldLocation']+['newLocation'])
1313

14+
print filePath
15+
1416
for root, dirs, files in os.walk(filePath, topdown=True):
1517
print root
1618
number = 1

0 commit comments

Comments
 (0)