Forum: Ruby Reg. Expressions with file directories

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7007f89eaa00d75c6733723de5aaff23?d=identicon&s=25 Trick Nick (silentd3at4)
on 2008-10-03 07:54
Im writing this program that is suppose to rename files using regular
expressions. I am suppose to pass 3 line arguments: the directory in
which to rename files, a regular expression that matches files to be
renamed, and a new extension to add to the end of files. It should run
as follows:
./fixname dir 'pattern' .ext

Im really really confused on reg. expressions and file directories so if
someone can point me in the right direction or help me get started this
would be greatly appreciated.  I understand that ARGV is an array or
arguments passed into the program so it should be something like
currentDirectory = Dir.new("#{ARGV[0]}) and then I should prolly do
somethign along the lines as DirectoryEntries =
currentDirectory.sort_entries

if DirectoryEntries[i].include?('pattern or ARGV[1]') == true
  then DirectoryEntries.sub("pattern", "#{ARGV[3]")
else
  puts directoryEntries[i]

I dunno someone with a bit more knowledge can you please help me with
syntax, or logical errors in my assumptions I would really appreciate it
THANKS.
7007f89eaa00d75c6733723de5aaff23?d=identicon&s=25 Trick Nick (silentd3at4)
on 2008-10-03 08:21
Here is what i have got so far, but I need to figure out how to rename
the file in that directory

#!/usr/bin/ruby

currentDir = Dir.new("#{ARGV[0]}")
dirEntries = currentDir.entries.sort
i = 0

dirEntries.each do |file|
#Get pattern and test, then replace if the file contains the specified
pattern
#Argument sake say ARGV1 = .txt i need to rename file.txt to be
file.text
   if file.include?("#{ARGV[1]}")
       print "#{file} is a txt file need to rename to .text\n"
   else
       print "This file doesnt need to be changed.\n"
   end

print "#{dirEntries[i]} \n"
    i += 1
end
7007f89eaa00d75c6733723de5aaff23?d=identicon&s=25 Trick Nick (silentd3at4)
on 2008-10-03 10:10
Here is my solution to my problem, just thought I would share just
incase someone else runs into a similar problem or is having similar
issues.

#first require the file utils, then take argument 1 or ARGV[0] and do a
Dir.new ...to work on the given directory.  Then get the entries(files)
to test to see  ...if they match the patterns given in the command line.
require 'fileutils'
givenDirectory = Dir.new("#{ARGV[0]}")
dirEntries = givenDirectory.entries.sort
i = 0

#Go through each file, or dirEntry and see if that file matches the
pattern     ...given.
dirEntries.each do |file|
    if file.include?("#{ARGV[1]}") == true
#store the new file's name by using the gsub in order to replace the
given      ...pattern(ARGV[1]) with the desired pattern from the
commandline, or ARGV[2]
       newFileName = dirEntries[i].gsub("#{ARGV[1]}", "#{ARGV[2]}")
#using the new file name copy the orginal file, with the new file name.
       FileUtils.cp("#{ARGV[0]}\/#{file}", "#{ARGV[0]}\/#{newFileName}")
#rm the old file.
       print "File:#{file} \>\> NewFile:#{newFileName} \n"
       FileUtils.rm("#{ARGV[0]}\/#{file}")
       else
       print "#{file} DIDN'T CHANGE.\n"
   end
    i += 1
end
This topic is locked and can not be replied to.