Skip to content

Instantly share code, notes, and snippets.

@sheepeeh
Last active August 29, 2015 14:02
Show Gist options
  • Save sheepeeh/3a2905f33ce0c2eefdc1 to your computer and use it in GitHub Desktop.
Save sheepeeh/3a2905f33ce0c2eefdc1 to your computer and use it in GitHub Desktop.
Rename files in the current directory based on a tab-delimited file.
# REQUIRES os_dir, available at https://gist.github.com/sheepeeh/39c25bd67ccc09ad78a0
# place in an easy to remember directory or add to your PATH. Expects (C/T)SV headings "oldname" and "newname."
# USAGE: filename.rb file_with_filenames.csv
require 'csv'
require_relative 'os_dir'
def rename_files(source)
current_dir
source = "#{@current_dir}/#{source}"
abort "Include a valid CSV file." unless File.exists?(source)
CSV.foreach(source, :headers => true, :header_converters => :symbol, :col_sep => "\t") do |line|
o_name = "#{@current_dir}/#{line[:oldname]}"
n_name = "#{@current_dir}/#{line[:newname]}"
File.rename(o_name,n_name)
puts "Renamed #{o_name}\tto\t#{n_name}"
end
end
rename_files(ARGV.shift)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment