-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename.rb
More file actions
49 lines (40 loc) · 1.48 KB
/
rename.rb
File metadata and controls
49 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# A script to take a filename and use indexConverter to change it.
# Usage
# 1. Navigate to the BalyProjectCode folder in a terminal
# 2. Open the folder containing image files
# 3. Right click and select 'copy as path'
# 4. In terminal, type `ruby rename.rb'
# 5. Paste the copied path into the terminal
# 6. Press enter
require 'fileutils'
load "indexConverter.rb"
def rename(old_filename)
classification = old_filename[0..old_filename.index("_")+3].gsub("_",".")
new_classification = indexConverter(classification)
new_filename = new_classification.to_s.gsub(".","_") + ".jpg"
return new_filename
end
def batch_rename_folder(folder_path)
# Replace backslashes (from windows filepaths)
if folder_path.include?("\\")
folder_path = folder_path.gsub("\\","/")
end
puts "renaming"
# Create the "renames" directory (if it doesn't already exist)
rename_folder = File.join(folder_path, "renames")
FileUtils.mkdir_p(rename_folder)
files = Dir.glob(File.join(folder_path, "*"))
files.each do |file_path|
# Skip directories
next if File.directory?(file_path)
old_filename = File.basename(file_path)
new_filename = rename(old_filename)
new_file_path = File.join(rename_folder, new_filename)
# Copy the file to the new location instead of renaming in place
FileUtils.copy_file(file_path, new_file_path)
puts "Copied #{old_filename} to #{new_filename} in the renames folder"
end
end
currentfolder = ARGV[0]
batch_rename_folder(currentfolder)
puts "Finished"