On Mon, May 9, 2011 at 3:45 PM, flebber <flebber.crue / gmail.com> wrote:
> 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()
> =A0dir =3D 'C:\Users\RenshawFamily\maven\Music\Foo Fighters\Live At
> Wembley Stadium'

Better uses forward slashes to avoid potential issues with quoting.

> =A0# add files to array unless a directoy is found
> =A0myFiles =3D Array.new << File.split(dir) unless File.file?("")

You are adding two elements (dirname and basename) to a new Array.  What fo=
r?

Also, what is the test at the end of the line supposed to do?

> =A0# Todo when files process step into directory and redo.
> =A0myFiles.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.

> =A0 =A0# remove leading numbers a "-"
> =A0 =A0file.replace(/dd\-/, '')

You rather want

# all numbers yield one dash
new_file =3D file.sub /\A\d+/, '-'

or

# each number gets its own dash
new_file =3D file.sub(/\A\d+/) {|m| '-' * m.length}

> =A0 =A0Fileutils.mv(myFile, file)

As far as I can see myFile is undefined here.

> =A0end
> end

Overall the logic is quite dark to me...

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/