From: "M. Edward (Ed) Borasky" Date: 2006-11-27T05:23:03+09:00 Subject: Re: How to re-name a file from Ruby script Li Chen wrote: > 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 >> > > 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.