![]() ![]() I removed the origin variable entirely to avoid the temptation to misuse it.įor your example folder structure, entering *.jpg, a, and b for the three input prompts (respectively) should rename the files as you are expecting. Since it's a fragment of a filename, it can't be used with rename. Use entry (that is, the complete filename) in the calls to gsub and rename instead of origin. ![]() Use the user-provided search pattern in the glob call instead of globbing for everything and then manually filtering it later.Otherwise, this newline character will mess up all of your match attempts. strip to remove the trailing newline that you get from gets. Puts "Rename from " + entry + " to " + newEntry If File.basename(entry, File.extname(entry)).include?(target) Slightly modified version: puts "Enter the file search query"ĭir.glob(searchPattern).sort.each do |entry| Puts "Rename from " + origin + " to " + newEntry ![]() NewEntry = origin.gsub(target, newTarget) Origin = File.basename(entry, File.extname(entry)) The Ruby script should be able to rename it to b1.jpg, b2.jpg, b3.jpg #!/Users/Antony/.rvm/rubies/ruby-1.9.3-p194/bin/ruby So can someone please take a look at see what I did wrong? Thanks a bunch in advance!Īssuming that in current folder there are three files: a1.jpg, a2.jpg, and a3.jpg If I comment out the include? check, gsub() does not seem to generate a new file name at all (i.e. include? method always returns false even though I see the filename contains such search pattern. I have been trying to work out a file rename program based on ruby, as a programming exercise for myself (I am aware of rename under linux, but I want to learn Ruby, and rename is not available in Mac).įrom the code below, the issue is that the. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |