# Reg. Expressions with file directories

**URL:** <https://www.ruby-forum.com/t/reg-expressions-with-file-directories/150074>\
**Category:** Ruby\
**Created:** [October 3, 2008, 7:54am UTC](https://www.ruby-forum.com/t/reg-expressions-with-file-directories/150074 "2008-10-03T07:54:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![silentd3at4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@silentd3at4](https://www.ruby-forum.com/u/silentd3at4)\
**Post date:** [October 3, 2008, 7:54am UTC](https://www.ruby-forum.com/t/reg-expressions-with-file-directories/150074/1 "2008-10-03T07:54:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![silentd3at4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@silentd3at4](https://www.ruby-forum.com/u/silentd3at4)\
**Post date:** [October 3, 2008, 8:21am UTC](https://www.ruby-forum.com/t/reg-expressions-with-file-directories/150074/2 "2008-10-03T08:21:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![silentd3at4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@silentd3at4](https://www.ruby-forum.com/u/silentd3at4)\
**Post date:** [October 3, 2008, 10:10am UTC](https://www.ruby-forum.com/t/reg-expressions-with-file-directories/150074/3 "2008-10-03T10:10:48Z")

</div>

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
