Created
July 5, 2020 06:36
-
-
Save kalpesh-fulpagare/fb49d67944101cb313098080280c2e23 to your computer and use it in GitHub Desktop.
Ruby Script to get timestamp from FIlename & rename the file to a common format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
names = [] | |
Dir.glob('*').select { |f| File.file?(f)}.each do |fn| | |
file_name = fn.gsub('_', '') | |
timestamp = Time.parse(file_name[file_name.index('20200'), 14]) | |
ext = file_name.split(".").last.downcase | |
if ext == "mp4" | |
new_file_name = timestamp.strftime("VID%Y%m%d-%H%M%S") | |
else | |
new_file_name = timestamp.strftime("IMG%Y%m%d-%H%M%S") | |
end | |
new_file_name = new_file_name+'.'+ext | |
if names.include?(new_file_name) | |
new_file_name = new_file_name + '_2.' + ext | |
end | |
if names.include?(new_file_name) | |
puts "Failed for: #{new_file_name}" | |
exit | |
end | |
names.push(new_file_name) | |
if (fn != new_file_name) | |
puts "mv #{fn} #{new_file_name}" | |
system "mv #{fn} #{new_file_name}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment