[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Copying files and renaming dupes

Stuart Clarke

4/15/2009 9:33:00 AM

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
--
Posted via http://www.ruby-....

6 Answers

Robert Klemme

4/15/2009 9:39:00 AM

0

2009/4/15 Stuart Clarke <stuart.clarke1986@gmail.com>:
> 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?

> =A0 =A0 =A0dsFile =3D File.copy(file, '\\Test')
> =A0 =A0 =A0File.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.

> =A0 =A0else
> =A0 =A0 =A0File.copy(file, '\\Test')
> =A0 =A0end
>
> 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

matt

4/15/2009 12:56:00 PM

0

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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Stuart Clarke

4/17/2009 7:17:00 AM

0

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.

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

Robert Klemme

4/18/2009 7:23:00 AM

0

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

Stuart Clarke

5/21/2009 12:02:00 PM

0

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

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

Robert Klemme

5/24/2009 3:40:00 PM

0

On 21.05.2009 14:01, Stuart Clarke wrote:
> 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|

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

> 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.

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...