Skip to content

Instantly share code, notes, and snippets.

@hswolff
Last active December 13, 2015 20:08
Show Gist options
  • Save hswolff/4967950 to your computer and use it in GitHub Desktop.
Save hswolff/4967950 to your computer and use it in GitHub Desktop.
This little script takes a directory, scans it for all files, and puts all the files into a directory of the same name.
#!/usr/bin/env node
fs = require 'fs'
dir = process.argv[2]
errorMsg = """
Error: need a directory
Please append path as first argument
Ex) coffee files2folder.coffee /Users/hswolff/Downloads
"""
throw errorMsg unless dir
console.log '\n********'
console.log 'Welcome to Files 2 Folder\n\nThe little app that puts your files, into folders'
console.log '********\n'
try
files = fs.readdirSync(dir)
catch e
throw e
filesMoved = 0
for file in files
fullPathFile = "#{dir}/#{file}"
stat = fs.statSync fullPathFile
if stat.isDirectory() or file.indexOf('.') is 0
# console.log "#{file} skipped"
continue
extIndex = file.lastIndexOf('.')
fileWithoutExtension = file.substr(0,extIndex)
newDirectory = "#{dir}/#{fileWithoutExtension}"
try
fs.mkdirSync newDirectory
fs.renameSync fullPathFile, "#{newDirectory}/#{file}"
console.log "#{file} moved into directory"
filesMoved++
catch e
console.log e.message
showAsterisks = (count) ->
return console.log 'Done!' unless count
console.log '*'
count--
anotherAsterisk = ->
setTimeout ->
showAsterisks(count)
, 500
anotherAsterisk()
console.log "\n#{filesMoved} files moved\n"
showAsterisks(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment