Hi,

I actually want to copy the contents of all files that is in a
particular directory into a new directory.The new files should be of a
specific size (2MB)and each with a new name (randomly assigned).

My Current Code looks,

testdir = "K:/test"
# Iterating over each entry
Dir.entries(testdir).each do |file|

# Joining the path where the files are

path = File.join testdir, file
if File.file? path
File.open path
File.copy(file,'K:/t1')
# NOTE: t1 file doesn't exist assuming
will create itself? probably wrong

end
end
end

When simply trying to copy using File.copy(to,from) it gives following
error message "No such file or directory K:/"

Do you think this approach is completely wrong ?
Any suggestions about giving random names for the new files..?

Many Thanks,

Recommended Answers

All 2 Replies

Thanks ,the link to the resource you gave was very helpful.
I can copy the files now, but when i am putting a restriction on a file size it doesn't care about the restriction and copies beyond the placed restriction.. My program logic is probably incorrect..! Here is the code,

Dir.entries(testdir).each do |file|
# Joining the path where the actual files are
path = File.join testdir, file
if File.file? path
File.open path do |source|
while (line=source.gets)
File.open("K:/testresult/test1.txt",'a') do |dest|
# Trying to put a restriction here if size of file is < 50
if File.size("K:/testresult/test1.txt")<50
dest.puts(line)
else
File.open("K:/testresult/test2.txt",'a')
dest.puts(line)
end
end
end end
It isn't giving any error message works fine but it is copying the file contents even if the file.size is beyond what i specified <50

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.