Tag Archives: ruby

Dropzone Script for Quick File Organization

I am guilty of using my downloads folder for a catch all of random unorganized files. A folder cluttered up with ISOs PDFs DMGs and a multitude of others.

So wrote a quick dropzone script that will help me at least organize a folder of random files. I didn’t need it to be sophisticated, I just wanted to gather all files of a certain extension together.

It only takes one extension at a time, but in the future could be modified to take multiple, or maybe read from a config file for autosorting

BEFORE

Screen Shot 2015-02-06 at 8.13.40 AM

RUN THE SCRIPT

Screen Shot 2015-02-06 at 8.14.54 AM

AFTER

Screen Shot 2015-02-06 at 8.16.00 AM

 

The code is below, as well as located on my github.

# Dropzone Action Info
# Name: Cleanup
# Description: Cleanup a folder based on extension
# Handles: Files
# Events: Dragged
# Creator: Michael Evans
# URL: https://sites.udel.edu/mdevans
# Version: 1.0
# RunsSandboxed: No
# UniqueID: 55
# MinDropzoneVersion: 3.0

def dragged

require 'fileutils'

ext = $dz.inputbox("Extension", "Please enter the file extension", "Extension")
ext = ext.chomp
ext = ext.tr('.','')
count = 0
$items.each do |dirName|

if File.directory?(dirName)

newDir = ext+"s"

ext = "*."+ext

Dir.mkdir(dirName+"/"+newDir) unless File.exists?(dirName+"/"+newDir)
$dz.begin("moving files")
searchPath = dirName + '/**/'+ ext
Dir[searchPath].reject{ |f| f[dirName+'/'+newDir]}.each do |filename|
if File.file?(filename)

if (FileUtils.mv(filename, dirName+"/"+newDir) unless File.exists?(dirName+"/"+newDir+"/"+File.basename(filename)))
count+=1
end
end
end

end

end
$dz.finish("moved #{count} files")
end