Forum: Ruby Copying files and renaming dupes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-04-15 11:32
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-15 11:39
(Received via mailing list)
2009/4/15 Stuart Clarke <stuart.clarke1986@gmail.com>:
>
> 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
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-04-15 15:01
(Received via mailing list)
Stuart Clarke <stuart.clarke1986@gmail.com> 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.
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-04-17 09:16
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 Clarke <stuart.clarke1986@gmail.com> 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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-18 09:25
(Received via mailing list)
On 17.04.2009 09:16, Stuart Clarke 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
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-05-21 14:01
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 Klemme wrote:
> On 17.04.2009 09:16, Stuart Clarke 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-24 17:42
(Received via mailing list)
On 21.05.2009 14:01, Stuart Clarke wrote:
> 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|

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
This topic is locked and can not be replied to.