Ruby Regex

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'
  # add files to array unless a directoy is found
  myFiles = Array.new << File.split(dir) unless File.file?("")
  # Todo when files process step into directory and redo.
  myFiles.each do|file|
    # remove leading numbers a "-"
    file.replace(/dd\-/, '')
    Fileutils.mv(myFile, file)
  end
end

I think you mean file.replace(/\d\d\-/,'') rather than file.replace(/dd\-/,'')

/dd\-/ will only match the character 'd' twice followed by a '-'

···

On 9 May 2011 14:45, 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()
dir = 'C:\Users\RenshawFamily\maven\Music\Foo Fighters\Live At
Wembley Stadium'
# add files to array unless a directoy is found
myFiles = Array.new << File.split(dir) unless File.file?("")
# Todo when files process step into directory and redo.
myFiles.each do|file|
# remove leading numbers a "-"
file.replace(/dd\-/, '')
Fileutils.mv(myFile, file)
end
end

instead of dd, try \d\d
file.replace(/\d\d\-/, '')

in irb:

"12-test".match(/\d\d\-/)
=> #<MatchData "12-">

···

On Mon, 9 May 2011 22:45:29 +0900 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()
  dir = 'C:\Users\RenshawFamily\maven\Music\Foo Fighters\Live At
Wembley Stadium'
  # add files to array unless a directoy is found
  myFiles = Array.new << File.split(dir) unless File.file?("")
  # Todo when files process step into directory and redo.
  myFiles.each do|file|
    # remove leading numbers a "-"
    file.replace(/dd\-/, '')
    Fileutils.mv(myFile, file)
  end
end

--
http://spiralofhope.com

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/

You can simplify this enormously by using Dir#glob, like so:

files = Dir.glob("C:/Users/RenshawFamily/maven/Music/Foo Fighters/Live
At Wembley Stadium/*.mp3") #change for the format you use

This way, you get only MP3 files (or whatever ending you specify) in
an Array. Together with the regex advice, you'll have less of a
headache with the script. :wink:

···

On Mon, May 9, 2011 at 3:45 PM, flebber <flebber.crue@gmail.com> wrote:

require 'fileutils'

def cleanFiles()
dir = 'C:\Users\RenshawFamily\maven\Music\Foo Fighters\Live At
Wembley Stadium'
# add files to array unless a directoy is found
myFiles = Array.new << File.split(dir) unless File.file?("")

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

File.split() doesn't do what you think it does, namely it does not
retrieve the files from the dir you specify. I'm not sure why you think
a method named split() would retrieve all the files from a directory.

···

--
Posted via http://www.ruby-forum.com/.

@all good feedback much appreciated will work on it further and get
back with some results.

sayth

···

On May 10, 5:34 am, 7stud -- <bbxx789_0...@yahoo.com> wrote:

File.split() doesn't do what you think it does, namely it does not
retrieve the files from the dir you specify. I'm not sure why you think
a method named split() would retrieve all the files from a directory.

--
Posted viahttp://www.ruby-forum.com/.

File.split() doesn't do what you think it does, namely it does not
retrieve the files from the dir you specify. I'm not sure why you think
a method named split() would retrieve all the files from a directory.

--
Posted viahttp://www.ruby-forum.com/.

@Robert

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|

Well in this first draft this check was just to ensure that no
directory were processed, but I wanted to develop it into a nested
test so the program would automatically process files in
subdirectories.

I need to review file.split for a suitable replacement.

so speaking it out, and I no there is errors but I am off reading RDOC
at moment :slight_smile: but this was the basic concept of where I was going.

myFiles = Array.new << File.split(dir) unless File.file?("") then
mydirs << Array.new
for myFiles in mfdirs.each do |file|
yadda yadda.

Sayth

···

On May 10, 5:34 am, 7stud -- <bbxx789_0...@yahoo.com> wrote:

File.split() doesn't do what you think it does, namely it does not
retrieve the files from the dir you specify. I'm not sure why you think
a method named split() would retrieve all the files from a directory.

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|

OK, if you want to process multiple files you basically have these options:

Dir
Dir.glob
Find.find

The advantage of the first two being that you can use a glob pattern
to select files and do not have to do it manually. Try this in IRB

Dir.glob('**/[0-9]*') {|f| p f, f.sub(/\A\d+/, '-')}

Well in this first draft this check was just to ensure that no
directory were processed, but I wanted to develop it into a nested
test so the program would automatically process files in
subdirectories.

I need to review file.split for a suitable replacement.

so speaking it out, and I no there is errors but I am off reading RDOC
at moment :slight_smile: but this was the basic concept of where I was going.

myFiles = Array.new << File.split(dir) unless File.file?("") then
mydirs << Array.new
for myFiles in mfdirs.each do |file|
yadda yadda.

I am sorry, this is not a concept but few lines of source code. As
long as your are not yet familiar with functionality of the language
it's probably easier to write down an algorithm in English words.
This also helps us understand what you are attempting to do. Only
then we be helpful.

Kind regards

robert

···

On Tue, May 10, 2011 at 5:35 AM, flebber <flebber.crue@gmail.com> wrote:

On May 10, 5:34 am, 7stud -- <bbxx789_0...@yahoo.com> wrote:

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