From: John Joyce Date: 2007-07-10T15:39:21+09:00 Subject: Re: Method to prevent writing over a file 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-forum.com/. > 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