How to Mass Rename Files in Ruby

How to mass rename files in ruby

Slightly modified version:

puts "Enter the file search query"
searchPattern = gets.strip
puts "Enter the target to replace"
target = gets.strip
puts "Enter the new target name"
newTarget = gets.strip
Dir.glob(searchPattern).sort.each do |entry|
if File.basename(entry, File.extname(entry)).include?(target)
newEntry = entry.gsub(target, newTarget)
File.rename( entry, newEntry )
puts "Rename from " + entry + " to " + newEntry
end
end

Key differences:

  • Use .strip to remove the trailing newline that you get from gets. Otherwise, this newline character will mess up all of your match attempts.
  • Use the user-provided search pattern in the glob call instead of globbing for everything and then manually filtering it later.
  • Use entry (that is, the complete filename) in the calls to gsub and rename instead of origin. origin is really only useful for the .include? test. Since it's a fragment of a filename, it can't be used with rename. I removed the origin variable entirely to avoid the temptation to misuse it.

For your example folder structure, entering *.jpg, a, and b for the three input prompts (respectively) should rename the files as you are expecting.

I need to rename part of the name of multiple files, as per user's indication

old_part = "Empty"

puts "Indicate new name of files":
new_name = gets.chomp

# Look for the specific files
Dir.glob("*#{old_part}*.txt").each do |renaming|
full_new_name = renaming.sub(/\A(.*)#{old_part}(.*)\z/, "\\1#{new_name}\\2")
File.rename(renaming, full_new_name)
end

What you were missing was to properly build the new name of file, changing old_name to new_name.

Ruby: Rename all files within a user given directory from .txt to text

As I understand from Your code,

1) You were not calling rename function

2) Your rename function had unnecessary checking of newfile, currentdir - if You want execute from current dir, it will understand "." symbol and will do glob in current dir.




I've moved pathname != "q" to outside of rename because of rename must do renaming, nothing else.

So final code:

#!/usr/bin/ruby
#This program was written by me

require 'fileutils'

def rename(pathname)
if File.directory?(pathname.to_s)
puts "renaming files in path"+pathname.to_s
Dir.glob(File.dirname(pathname.to_s)+"/*.txt").each do |f|
FileUtils.mv f, "#{File.dirname(f)}/#{File.basename(f,'.*')}.text"
puts "#{File.basename} => #{File.basename(f,'.*')}.text"
end
else
puts "Invalid Path"
end
end

puts "what directory would you like to change? "
pathname = gets.chomp

if pathname != "q"
rename(pathname)
end




p.s. normal user will just call:

rename -S .txt .text /path/to/files/*.txt

or:

rename -S .txt .text ./*.txt

How to rename particular files in the folder for ruby

This is going to be trick, here I am using FileUtils.mv method.

path = "/home/itsme/videos"
Dir.open(path).each do |p|
next if File.extname(p) != ".mp4"
filename = File.basename(p, File.extname(p))
newname = filename.upcase + File.extname(p)
FileUtils.mv("#{path}/#{p}", "#{path}/#{newname}")
end

To use FileUtils class method you have to include it by using require fileutils

ruby rename file with multiple string format

You can iterate through files with Dir#glob
From the description : "Note that this pattern is not a regexp, it’s closer to a shell glob"

Dir.glob(pattern) each do |f|
#do something with f
end

where pattern could be "*" or "*.txt".
You'll need to give more information about your search.



Related Topics



Leave a reply



Submit