safe move/copy with incrementing filenames?

P

Paul Archer

So, I'm working on some DAM (digital asset management) scripts, and I want
to make sure that I don't overwrite any files when I move or copy them.
I was about to start working on a move method that would add a number to
the destination filename if the name already exists. (IE
mv('pma_20090720_1234.jpg', 'archive/2009/07') might create an
'archive/2009/07/pma_20090720_1234-1.jpg if pma_20090720_1234.jpg exists.)

Question: is there already a module that can do that?

Paul
 
P

pharrington

So, I'm working on some DAM (digital asset management) scripts, and I want
to make sure that I don't overwrite any files when I move or copy them.
I was about to start working on a move method that would add a number to
the destination filename if the name already exists. (IE
mv('pma_20090720_1234.jpg', 'archive/2009/07') might create an
'archive/2009/07/pma_20090720_1234-1.jpg if pma_20090720_1234.jpg exists.)

Question: is there already a module that can do that?

Paul

I've just been doing this:

def unique(filename)
count = 0
unique_name = filename
while File.exists?(unique_name)
count += 1
unique_name = "#{File.join(
File.dirname(filename),
File.basename(filename, ".*"))}_#{count}#{File.extname
(filename)}"
end
unique_name
end

there's probably a better way though
 

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

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top