test.rb
· 666 B · Ruby
Raw
require 'fileutils'
# Set the directory path
dir_path = "./cleanup-tiff-posts/"
# 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!"
1 | require 'fileutils' |
2 | |
3 | # Set the directory path |
4 | dir_path = "./cleanup-tiff-posts/" |
5 | |
6 | # Define a regex to match common date formats in filenames |
7 | date_pattern = /(\d{4}-\d{2}-\d{2})|(\d{2}-\d{2}-\d{4})/ |
8 | |
9 | # Loop through each file in the directory |
10 | Dir.foreach(dir_path) do |filename| |
11 | next if filename == '.' || filename == '..' |
12 | |
13 | new_filename = filename.gsub(date_pattern, '').gsub('__', '_').strip |
14 | new_path = File.join(dir_path, new_filename) |
15 | |
16 | # Rename the file if the name has changed |
17 | if new_filename != filename |
18 | FileUtils.mv(File.join(dir_path, filename), new_path) |
19 | puts "Renamed: #{filename} -> #{new_filename}" |
20 | end |
21 | end |
22 | |
23 | puts "Date removal complete!" |