[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to re-name a file from Ruby script

Li Chen

11/26/2006 8:12:00 PM

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

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

5 Answers

matt

11/26/2006 8:20:00 PM

0

Li Chen <chen_li3@yahoo.com> 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

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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Brad Tilley

11/26/2006 8:23:00 PM

0

Quoting Li Chen <chen_li3@yahoo.com>:

> 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

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)

M. Edward (Ed) Borasky

11/26/2006 8:23:00 PM

0

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

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


Stefano Crocco

11/26/2006 8:24:00 PM

0

Alle 21:12, domenica 26 novembre 2006, Li Chen ha scritto:
> 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

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

11/26/2006 8:49:00 PM

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

Stefano,

Thanks and method File.rename is what I want.

Li

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