I am new too using regex on files and I am not quite getting the
result I expect. The script currently doesn't error but also doesn't
update the file either.
Trying to remove leading numbers and ' - 'from the start of a filename
given a directory.
require 'fileutils'
def cleanFiles()
dir = 'C:\Users\RenshawFamily\maven\Music\Foo Fighters\Live At
Wembley Stadium'
Better uses forward slashes to avoid potential issues with quoting.
# add files to array unless a directoy is found
myFiles = Array.new << File.split(dir) unless File.file?("")
You are adding two elements (dirname and basename) to a new Array. What for?
Also, what is the test at the end of the line supposed to do?
# Todo when files process step into directory and redo.
myFiles.each do|file|
Here you are iterating through an Array which you know to always only
contain 1 element (a nested Array with two elements, see above). It
does not make any sense to use an Array.
# remove leading numbers a "-"
file.replace(/dd\-/, '')
You rather want
# all numbers yield one dash
new_file = file.sub /\A\d+/, '-'
or
# each number gets its own dash
new_file = file.sub(/\A\d+/) {|m| '-' * m.length}
Fileutils.mv(myFile, file)
As far as I can see myFile is undefined here.
end
end
Overall the logic is quite dark to me...
Kind regards
robert
···
On Mon, May 9, 2011 at 3:45 PM, flebber <flebber.crue@gmail.com> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/