Skip to content

Instantly share code, notes, and snippets.

@swape
Last active August 29, 2015 14:26
Show Gist options
  • Save swape/875485fa5c39c86335e5 to your computer and use it in GitHub Desktop.
Save swape/875485fa5c39c86335e5 to your computer and use it in GitHub Desktop.
ruby script for moving files to directories based on [filetype]/[year]/[month]/[filename] from recovered files on mac using PhotoRec.
puts '----Starting the move'
dir_target = '/Volumes/backup/recfiles'
#moving the files
def moveFiles (myfile)
fileType = File.extname(myfile)[1..-1]
targetPath = "/Volumes/backup/recovered/#{fileType}/";
fileTime = File.mtime(myfile)
fileYear = Time.at(fileTime).year
fileMonth = Time.at(fileTime).month
fileName = File.basename(myfile)
targetDir = "\"#{targetPath}#{fileYear}/#{fileMonth}\""
unless File.directory?("#{targetPath}#{fileYear}/#{fileMonth}")
cmdPathMaker = "mkdir -p #{targetDir}"
if not fileMonth == ''
system(cmdPathMaker)
end
end
cmdToExec = "mv -fn \"#{myfile}\" #{targetDir}/"
if not fileMonth == ''
system(cmdToExec)
end
end
# listing the files
def listFiles (f)
print "-----DIR: #{f} "
mycounter = 1
Dir.glob("#{f}/**/*").each do |f|
if not File.extname(f)[1..-1] == ''
moveFiles f
mycounter = mycounter.next
end
end
p " ---- #{mycounter} files."
end
# list the dirs
Dir.glob("#{dir_target}/**/*").each do |f|
if File.directory?(f)
listFiles f
end
end
# cleaning up
system("find #{dir_target} -depth 1 -type d -empty -delete")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment