How to re-name a file from Ruby script

L

Li Chen

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
 
M

matt neuburg

Li Chen said:
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.
 
B

Brad Tilley

Quoting Li Chen said:
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

M. Edward (Ed) Borasky

Li said:
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
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.
 
S

Stefano Crocco

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

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
 
L

Li Chen

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
 

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
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top