2011-04-18 5 views
0

나는 조금 혼란 스럽다. 일련의 파일 중 md5 체크섬을 검사하는 프로그램을 작성 중입니다. 그 부분은 훌륭하게 작동합니다. 그 파일을 쉽게 참조/제거 할 수 있도록 중복 폴더로 옮기는 것은 멋지다고 생각했습니다. 문제는 계속해서 실패하는 것입니다. 파일이나 디렉토리가 없다고 말하면서이 파일을 올바르게 이동하려고하는지 확신 할 수 없습니다. 누군가가 한 번 봐도 괜찮다면 나는 감사 할 것입니다. 미리 감사드립니다.루비의 배열에서 참조 된 파일 이동하기

!/usr/local/bin/ruby 

require 'find' 
require 'digest/md5' 
require 'fileutils' 

testArray = Dir["**/**/*"]        #create an array based off the contents of the current directory 

def checksum(file)          #method for calculating the checksum 
    sumArray = Array.new 
    dupArray = Array.new 
    file.each do |file|          #Iterate over each entry in the array 
    digest = Digest::MD5.hexdigest(File.read(file))  # create a MD5 checksum for the file and storeit in the variable digest 
    dupArray << file if sumArray.include?(digest)   # check to see if the item exists in the sumarray already, if not ad to duparray 
sumArray << digest unless sumArray.include?(digest) # if it's not already in sumarray, add it in there 
    end 
    dupArray 
end 

this is where my problems start ;) 
def duplicateDirectory(file) 
file.each do |file| 
    FileUtils.mv('file', '/duplicate') if Dir.exists?('duplicate') 
    Dir.mkdir('duplicate') 
    FileUtils.mv('file', '/duplicate') 
    end 
end 


sumTest = checksum(testArray)       #pass the test array along to the method written 
puts sumTest 
duplicateDirectory(sumTest) 
+0

코드가 올바르게 표시되지 않았습니다. – theheirarchy

+0

모두 개선되었습니다. :) 나중에 참조 할 수 있도록 텍스트 편집기에는 두 개의 중괄호 {}가있는 코드 버튼이 있습니다. 그냥 텍스트를 강조 표시하고 해당 버튼을 누르십시오. ;) – kafuchau

답변

0

당신은 file 주위 ''를 제거하고 duplicate 앞에 /을 제거해야합니다. 그리고 다음과 같이 다시 작성하는 것이 좋습니다 :

dir = 'duplicate' 
Dir.mkdir(dir) if !Dir.exists?(dir) 
FileUtils.mv(file, dir) 
+0

어쩌면 FileUtils.mkdir_p (dir) – tokland

+0

대단히 감사합니다. 내 질문에 완전히 답했습니다. – theheirarchy

+0

'제외하다 '는 아닌'if!' –