Skip to content

Instantly share code, notes, and snippets.

@twhite96
Last active March 21, 2025 04:58
Show Gist options
  • Save twhite96/9d9d527bd9bf7150ee4be2c387883967 to your computer and use it in GitHub Desktop.
Save twhite96/9d9d527bd9bf7150ee4be2c387883967 to your computer and use it in GitHub Desktop.
delete dates from file
# spent forever trying to find a way to do this. Got pissed off and tired and used Chat jippity. Thanks Chat jippity.
require 'fileutils'
# Set the directory path
dir_path = "<dir>"
# Define a regex to match common date formats in filenames
date_pattern = /(\d{4}-\d{2}-\d{2})|(\d{2}-\d{2}-\d{4})/
# Loop through each file in the directory
Dir.foreach(dir_path) do |filename|
next if filename == '.' || filename == '..'
new_filename = filename.gsub(date_pattern, '').gsub('__', '_').strip
new_path = File.join(dir_path, new_filename)
# Rename the file if the name has changed
if new_filename != filename
FileUtils.mv(File.join(dir_path, filename), new_path)
puts "Renamed: #{filename} -> #{new_filename}"
end
end
puts "Date removal complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment