开发者

Moving a file referenced in an array in ruby

I'm a bit confused. I'm writing a program to check the md5 checksum of a series of files. That part works great. I thought it'd be cool though to move those files to a duplicate folder for easy reference/removal. The issue is it keeps failing, it says no such file or directory, and I'm not sure if I'm even trying to move this file correctly. if someone wouldn't mind taking a look I'd be appreciative. Thanks in advance.

!/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
开发者_JAVA技巧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)


You should remove '' around file and remove / in front of duplicate. And it's better to rewrite like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜