From: Robert Klemme Date: 2009-04-15T18:39:02+09:00 Subject: Re: Copying files and renaming dupes 2009/4/15 Stuart Clarke : > Hi all, > > I have a quick query. I am copying data from a hard drive into a folder > structure, however some of files have the same name and > therefore get overwritten by the most recent file with that name. I have > been using file.identical to establish if the files match and if they do > rename them with an incremental > number (file[1].txt, file[2].txt etc). > > This is my identical code > > if File.identical?(file, file) Do you really want to compare "file" with itself? >      dsFile = File.copy(file, '\\Test') >      File.rename(dsFile, "#{fileBase}#{x}.#{ext}") I see issues creeping up here, because you seem to rely on some script internal counters for your renaming operation: you should rather look into the directory and check whether a file with the given basename does exist already and if so generate a new target name. >    else >      File.copy(file, '\\Test') >    end > > This renames the file prior to copying, which is not what I want. I > basically want to copy files, if a file with the same name exists rename > it. Then why don't you just create the target name and do the copy with that? Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/