Reg. Expressions with file directories

T

Trick Nick

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.include?('pattern or ARGV[1]') == true
then DirectoryEntries.sub("pattern", "#{ARGV[3]")
else
puts directoryEntries

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.
 
T

Trick Nick

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} \n"
i += 1
end
 
T

Trick Nick

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.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top