Ruby Regex

F

flebber

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
 
P

Peter Hickman

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

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

spiralofhope

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

in irb:

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

Robert Klemme

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/
 
P

Phillip Gawlowski

require 'fileutils'

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

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

files =3D 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. ;)

--=20
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.
 
7

7stud --

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.
 
F

flebber

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.

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

sayth
 
F

flebber

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.

@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 :) 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
 
R

Robert Klemme

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

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 :) but this was the basic concept of where I was going.

myFiles =3D 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


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top