Skip to content

Instantly share code, notes, and snippets.

@suryart
Created May 23, 2017 19:26
Show Gist options
  • Save suryart/1ee9b240af44aa99c4be5b2ab4fc4828 to your computer and use it in GitHub Desktop.
Save suryart/1ee9b240af44aa99c4be5b2ab4fc4828 to your computer and use it in GitHub Desktop.
change duplicate file names
file_extension = '.rb'
files = Dir["./*#{file_extension}"]
puts "="*10
puts " Total files: #{files.count}"
puts "="*10
files_deleted = 0
file_timestamp = Time.now.strftime('%Y%m%d')
files.each do |file|
puts File.basename file, file_extension
fielname = File.basename file, file_extension
file_words = filename.split('_')
file_words[-2] = file_timestamp # second last
new_filename = file_words.join('_') + file_extension
# if file exists then check for its size
# if current file's size is greater than replace, otherwise move on
if File.exist?(new_filename)
if File.size(filename) < File.size(new_filename)
puts "deleting file #{new_filename} as its size: #{File.size(new_filename)} than new file found: #{filename} size: #{File.size(filename)}"
files_deleted += 1
# File.delete(new_filename)
end
end
puts "renaming file from #{filename} to #{new_filename}"
# File.rename(filename, new_filename)
# puts file_timestamp
# puts File.size(file)
end
puts "#{files_deleted} File deleted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment