Skip to content

Instantly share code, notes, and snippets.

@sri
Created May 16, 2011 05:09
Show Gist options
  • Save sri/973965 to your computer and use it in GitHub Desktop.
Save sri/973965 to your computer and use it in GitHub Desktop.
mv_ldown.js -- port of mv_ldown.rb to Node javascript
#! /usr/bin/env node
var path = require('path'),
fs = require('fs')
var userhome = process.env['HOME'],
downloadsDir = path.join(userhome, 'Downloads'),
downloads = fs.readdirSync(downloadsDir),
all = []
downloads.forEach(function(f) {
f = path.join(downloadsDir, f)
all.push([f, fs.statSync(f)])
})
// sort by newest first
all.sort(function(x, y) {
var a = x[1].mtime.valueOf(),
b = y[1].mtime.valueOf()
if (a < b) return 1
else if (a > b) return -1
else return 0
})
var lastDownload = null,
base = null
all.forEach(function(x) {
if (lastDownload == null && x[1].isFile()) {
lastDownload = x[0]
base = path.basename(lastDownload)
}
})
if (lastDownload == null) {
console.log("no files in ~/Downloads")
} else if (path.existsSync(base)) {
console.log("./" + base + " already exists")
} else {
console.log("moving " + lastDownload + " to .")
fs.renameSync(lastDownload, path.basename(lastDownload))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment