Stefano Crocco
11/26/2006 8:24:00 PM
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