Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare 2 directories

Status
Not open for further replies.

liondrys

Programmer
Sep 10, 2003
2
FR
Hi all,

I will try to explain my problem. i'm french, well, sorry for grammatical mistakes :p

I try to compare the files in two directories : per Date / Length / Last modification. But, well, i'm noob in Ruby so i do something but it's not very powerful :-(, it just check files who are not in both directories ...

Here my coding for the moment:

# global variable

logfile = File.new("log_cservers.txt","w")

# Intialize the date/time when the script is launched

now = Time.new
now.localtime

# begin

print "Welcome in cservers ! you will able to compare directories in two servers. \n"
print "Make sure you have the two server connecting on your computer. If not, please do a network mapping. \n\n"

# Here you have to write the two directories between " "

serv1 = "F:\\Apriso\\FlexNet72\\Kernel"
serv2 = "K:\\Apriso\\FlexNet72\\Kernel"
print "#{serv1} VS #{serv2}\n\n"

# Here we look for the directories

if File.exist?(serv1)
print "#{serv1} ------> ON\n"
else print "#{serv1} ------> OFF\n"
end

if File.exist?(serv2)
print "#{serv2} ------> ON\n"
else print "#{serv2} ------> OFF\n"
end

# Here we collect files' names in the directories

@file_in_1 = Dir.entries(serv1)
@file_in_2 = Dir.entries(serv2)

# Files' comparaison - In logfile we note files who don't exist in serv1 AND serv2

$stdout = logfile

puts "----------\n"
puts now.ctime
puts "\n----------\n"
puts "File in #{serv1} and not in #{serv2}\n"
puts "----------\n\n"

@file_in_1.each do |x|
x_attr = serv1 << x
filexist = 0
i = 0
while i < @file_in_1.length do
filexist = 1 if x == @file_in_2
i += 1
end
puts &quot;#{x} \t Size=#{x_attr.size} \n&quot; if filexist == 0
end

puts &quot;\n----------\n&quot;
puts &quot;File in #{serv2} and not in #{serv1}\n&quot;
puts &quot;----------\n\n&quot;

@file_in_2.each do |x|
x_attr = serv2 << x
filexist = 0
i = 0
while i < @file_in_2.length do
filexist = 1 if x == @file_in_1
i += 1
end
puts &quot;#{x} \t Size=#{x_attr.size} \n&quot; if filexist == 0
end


Thanks in advance !

Liondrys
 
Well I did it ! :D
Maybye this little script is good if during a copy ( Mass copy ... maybe 100+ ) is stopped before the end and you want to know what is the files who are not copy.

# global variable

logfile = File.new(&quot;log_cservers.txt&quot;,&quot;w&quot;)

# Intialize the date/time when the script is launched

now = Time.new
now.localtime

# Here you have to write the two directories between &quot; &quot; like &quot;D:\\toto\\titi\\.&quot; /!\ Don't forget &quot;\\.&quot; /!
serv1 = &quot;D:\\levasseur\\ruby\\fox\\.&quot;
serv2 = &quot;D:\\levasseur\\ruby\\.&quot;

# Here we collect files' names in the directories

ini_1 = Dir.entries(serv1)
ini_2 = Dir.entries(serv2)
file_complete_path_1 = []
file_complete_path_2 = []
file_in_1 = []
file_in_2 = []
objfile1 = []
objfile2 = []

ini_1.each do |x|
pathx = File.dirname(serv1) << &quot;\\&quot; + x
if FileTest.file?(pathx)

file_complete_path_1 << pathx
file_in_1 << x
objfile1 << File.new(pathx)

end
end


ini_2.each do |x|
pathx = File.dirname(serv2) << &quot;\\&quot; + x
if FileTest.file?(pathx)

file_complete_path_2 << pathx
file_in_2 << x
objfile2 << File.new(pathx)

end
end

# --------------BEGIN---------------
$stdout = logfile

puts &quot;----------------------------------------&quot;
puts now.ctime
puts &quot;----------------------------------------&quot;
puts &quot;File in #{serv1} and not in #{serv2}&quot;
puts &quot;---------------------------------------- &quot;
puts &quot; &quot;

file_in_1.each do |x|
filexist = 0

file_in_2.each do |y|
filexist = 1 if x == y
end

puts &quot;#{x}&quot; if filexist ==0

end

puts &quot; &quot;
puts &quot;---------------------------------------- &quot;
puts &quot;File in #{serv2} and not in #{serv1}&quot;
puts &quot;---------------------------------------- &quot;
puts &quot; &quot;

file_in_2.each do |x|
filexist = 0

file_in_1.each do |y|
filexist = 1 if x == y
end

puts &quot;#{x}&quot; if filexist ==0

end

puts &quot; &quot;
puts &quot;---------------------------------------- &quot;
puts &quot;Same name but different size&quot;
puts &quot;---------------------------------------- &quot;
puts &quot; &quot;

i = 0
file_complete_path_1.each do |x|
filesize = 0
j = 0
file_complete_path_2.each do |y|
if file_in_1 == file_in_2[j]
if FileTest.size?(x) != FileTest.size?(y)
puts &quot;#{x} and #{y}&quot;
puts &quot;#{x} --> #{FileTest.size?(x)}&quot;
puts &quot;#{y} --> #{FileTest.size?(y)}&quot;
puts &quot; &quot;
end
end
j += 1
end
i += 1
end

puts &quot; &quot;
puts &quot;---------------------------------------- &quot;
puts &quot;Same name but different last modification&quot;
puts &quot;---------------------------------------- &quot;
puts &quot; &quot;

i = 0
file_complete_path_1.each do |x|
filesize = 0
j = 0
file_complete_path_2.each do |y|
if file_in_1 == file_in_2[j]
if objfile1.mtime != objfile2[j].mtime
puts &quot;#{x} and #{y}&quot;
puts &quot;#{x} --> #{objfile1.mtime}&quot;
puts &quot;#{y} --> #{objfile2[j].mtime}&quot;
puts &quot; &quot;
end
end
j += 1
end
i += 1
end





# -------------- END ------------

puts &quot; &quot;
puts &quot;---------------------------------------- &quot;
puts &quot; EOF &quot;
puts &quot;---------------------------------------- &quot;
puts &quot; &quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top