Copying files and renaming dupes

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)
dsFile = File.copy(file, ‘\Test’)
File.rename(dsFile, “#{fileBase}#{x}.#{ext}”)
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.

Many thanks

Stuart

2009/4/15 Stuart C. [email protected]:

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

Ok I must of misunderstood the docs for it, in reflection I am thinking
file.exist?

Also, Robert what do you mean by creating a target name?

Regards

Stuart
matt neuburg wrote:

Stuart C. [email protected] wrote:

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

But that is not what File.identical? does. m.

On 17.04.2009 09:16, Stuart C. wrote:

Ok I must of misunderstood the docs for it, in reflection I am thinking
file.exist?

Also, Robert what do you mean by creating a target name?

You copy from source to target.

robert

I have stepped back from this code for a while and I am still stuck.

My thinking is

  1. read source data set
  2. if item in source matches criteria
  3. move that file to target if a file with that name doesnt already
    exist in target
  4. if a file does exist with the same name in target rename the file
    with a sequential number eg test[1].txt
  5. then move the file to target

This is the relevant code I have

Find.find(dir) do |path|
if File.extname(path) == “.txt”
sourceFile.push File.basename(path)
sourceFile.each do |source|
if File.file?(path) and File.basename(path) != source
File.move(path, culled)
elsif
File.move(path, temp)
File.rename(tmp, “tester.exe”)
File.move(temp, target)
end
end
end
end

This just doesn’t do anything, not even error.

Thanks in advance.

Robert K. wrote:

On 17.04.2009 09:16, Stuart C. wrote:

Ok I must of misunderstood the docs for it, in reflection I am thinking
file.exist?

Also, Robert what do you mean by creating a target name?

You copy from source to target.

robert

On 21.05.2009 14:01, Stuart C. wrote:

  1. then move the file to target

This is the relevant code I have

Find.find(dir) do |path|
if File.extname(path) == “.txt”
sourceFile.push File.basename(path)
sourceFile.each do |source|

This looks strange: you push to “sourceFile” and then iterate
“sourceFile” but never remove anything from “sourceFile”.

This just doesn’t do anything, not even error.

The code looks a bit weird to me but one thing is certain: there is a
lot of code missing (what is “culled”? how is “sourceFile” initialized
etc.). It is hard to give any recommendations that way.

Kind regards

robert

Stuart C. [email protected] wrote:

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

But that is not what File.identical? does. m.