How to re-name a file from Ruby script

Hi all,

I have a folder containing many files with format
LIPID5-2.001
LIPID5-2.002

LIPID5-2.200

I want to change all of them into
LIPID5-3.001
LIPID5-3.002

LIPID5-3.200

I write a script as follows. Based on the screen output the filename is
changed. But I go to the same folder and find the original file
LIPID5-2.001 is still there and there is no file called LIPID5-3.001.
Any comments?

Thanks,

Li

path=‘C:\Ruby\self’

Dir.entries(path).each do |filename|

if filename=~/(LIPID5-2)(.\w+)/i
p filename.gsub!(/LIPID5-2/,‘LIPID5-3’)
end

end

##output

ruby dir9.rb
“LIPID5-3.001”
Exit code: 0

Quoting Li Chen [email protected]:

LIPID5-3.002
Li
end
You have to commit the rename. gsub is only acting on the strings that
are the
filenames it does not actually commit a file name change. Try something
like
this:

File.rename(old_name, new_name)

Li Chen wrote:

LIPID5-3.002
Li
end

I don’t see anything in the code that actually renames the file on the
hard drive. You’ve computed the new name but have you executed something
to actually change the name of the file?

By the way, unless you’ve literally only got a couple of hours to get
this working, directories with lots of coded file names that have to get
renamed to make sense to some other program is a very inefficient
design, and for “extra credit” you might want to look at a more cogent
re-design. Before there were industrial strength open source databases,
a lot of code like that got written. But these days, it’s silly, even if
you only have a few weeks, given how easy it is with frameworks like
ActiveRecord / Rails to use a real database.


M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)
http://borasky-research.blogspot.com/

If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.

Alle 21:12, domenica 26 novembre 2006, Li Chen ha scritto:

LIPID5-3.002
Li
end

##output

ruby dir9.rb

“LIPID5-3.001”

Exit code: 0

filename is only a ruby string, it has no relationship with your
filesystem.
If you want to change the name of a file, you should use the File.rename
method:

Dir.entries(path).each do |filename|
if filename=~/(LIPID5-2)(.\w+)/i
File.rename(filename, filename.gsub(/LIPID5-2/,‘LIPID5-3’))
end
end

Stefano

Li Chen [email protected] wrote:

LIPID5-3.002
Li

path=‘C:\Ruby\self’

Dir.entries(path).each do |filename|

if filename=~/(LIPID5-2)(.\w+)/i
p filename.gsub!(/LIPID5-2/,‘LIPID5-3’)
end

You’re changing a variable called filename but you’re not also talking
to the filesystem and asking it to change the name of the file on disk.
For that, you want something like File.rename.

m.

filename is only a ruby string, it has no relationship with your
filesystem.
If you want to change the name of a file, you should use the File.rename
method:

Dir.entries(path).each do |filename|
if filename=~/(LIPID5-2)(.\w+)/i
File.rename(filename, filename.gsub(/LIPID5-2/,‘LIPID5-3’))
end
end

Stefano

Stefano,

Thanks and method File.rename is what I want.

Li