Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Reg. Expressions with file directories
Nick Bo
10/3/2008 5:53:00 AM
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.
--
Posted via
http://www.ruby-...
.
2 Answers
Nick Bo
10/3/2008 6:20:00 AM
0
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
--
Posted via
http://www.ruby-...
.
Nick Bo
10/3/2008 8:10:00 AM
0
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
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Reg. Expressions with file directories
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password