[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Method to prevent writing over a file

12 34

7/10/2007 4:26:00 AM

Coming from OS X, I'm used to protection to overwriting files. I have
the need for such protection. Before I write it myself is there a
standard method for this? I'd like it work like OS X where if a file of
the same name exists, the file to be copied gets an index number. For
example, existingFile.txt already is in the dest directory, and a file
of same name being copied becomes existingFile1.txt.

I don't think even this newbie will find it difficult, but it seems with
all the File and FileUtil methods something like this woudd exist.

Thanks

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

5 Answers

Robert Klemme

7/10/2007 6:25:00 AM

0

2007/7/10, 12 34 <rubyforum@web.knobby.ws>:
> Coming from OS X, I'm used to protection to overwriting files. I have
> the need for such protection. Before I write it myself is there a
> standard method for this? I'd like it work like OS X where if a file of
> the same name exists, the file to be copied gets an index number. For
> example, existingFile.txt already is in the dest directory, and a file
> of same name being copied becomes existingFile1.txt.
>
> I don't think even this newbie will find it difficult, but it seems with
> all the File and FileUtil methods something like this woudd exist.

I believe IO::CREAT might do what you need. Try this

File.open("foo", IO::CREAT | IO::WRITE) do |io|
...
end

Kind regards

John Joyce

7/10/2007 6:39:00 AM

0


On Jul 9, 2007, at 11:26 PM, 12 34 wrote:

> Coming from OS X, I'm used to protection to overwriting files. I have
> the need for such protection. Before I write it myself is there a
> standard method for this? I'd like it work like OS X where if a
> file of
> the same name exists, the file to be copied gets an index number. For
> example, existingFile.txt already is in the dest directory, and a file
> of same name being copied becomes existingFile1.txt.
>
> I don't think even this newbie will find it difficult, but it seems
> with
> all the File and FileUtil methods something like this woudd exist.
>
> Thanks
>
> --
> Posted via http://www.ruby-....
>
You want to use the Dir class and File class.
using ri to check out these classes will give you lots of tools.

It's not terribly different in any language, except in Ruby, it's
easy to refactor your code into small readable functions (methods).

You might need a Regular Expression to split file extension and file
name before appending the number.
Once you have it working, expand its abilities by looking for
existing files with the same name and a number.

here's a little pseudo code to set you in the right direction:

read directory into array

if file_name.exists? in directory_name
append number to new_file_name
end

write new_file_name to directory

Ari Brown

7/10/2007 12:07:00 PM

0


On Jul 10, 2007, at 12:26 AM, 12 34 wrote:

> Coming from OS X, I'm used to protection to overwriting files. I have
> the need for such protection. Before I write it myself is there a
> standard method for this? I'd like it work like OS X where if a
> file of
> the same name exists, the file to be copied gets an index number. For
> example, existingFile.txt already is in the dest directory, and a file
> of same name being copied becomes existingFile1.txt.
>
> I don't think even this newbie will find it difficult, but it seems
> with
> all the File and FileUtil methods something like this woudd exist.

Try using File.flock method.

http://www.ruby-doc.org/core/classes/File.ht...

aRi
-------------------------------------------------------|
~ Ari
crap my sig won't fit


Bertram Scharpf

7/10/2007 1:04:00 PM

0

Hi,

Am Dienstag, 10. Jul 2007, 13:26:14 +0900 schrieb 12 34:
> Coming from OS X, I'm used to protection to overwriting files. I have
> the need for such protection. Before I write it myself is there a
> standard method for this? I'd like it work like OS X where if a file of
> the same name exists, the file to be copied gets an index number. For
> example, existingFile.txt already is in the dest directory, and a file
> of same name being copied becomes existingFile1.txt.

class File
class <<self
def safe_open name
i = 0
n = name
begin
f = File.new n, File::CREAT|File::EXCL|File::WRONLY
yield f
rescue Errno::EEXIST
i += 1
n = "#{name}_#{i}"
retry
ensure
f.close
end
end
end
end

File.safe_open "some_file" do |f|
puts "Generated filename is: #{f.path}"
f.puts some_data
end


Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Bertram Scharpf

7/10/2007 1:11:00 PM

0

Hi,

Am Dienstag, 10. Jul 2007, 22:03:52 +0900 schrieb Bertram Scharpf:
>
> def safe_open name
> i = 0
> n = name
> begin
> f = File.new n, File::CREAT|File::EXCL|File::WRONLY
> yield f
> rescue Errno::EEXIST
> i += 1
> n = "#{name}_#{i}"
> retry
> ensure
> f.close
> end
> end

Sorry, this is wrong. The yield should _not_ appear inside
the begin-rescue block as it could raise an exception
itself. Better is:

def safe_open name
i, n = 0, name
f = begin
File.new n, File::CREAT|File::EXCL|File::WRONLY
rescue Errno::EEXIST
i += 1
n = "#{name}_#{i}"
retry
end
yield f
ensure
f.close
end


Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...