Last active
December 13, 2015 20:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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