Copying files and renaming dupes

S

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)
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
 
R

Robert Klemme

2009/4/15 Stuart Clarke said:
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.rubybestpractices.com/
 
M

matt neuburg

Stuart Clarke said:
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.
 
S

Stuart Clarke

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
 
R

Robert Klemme

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
 
S

Stuart Clarke

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

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top