Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const path = require('path');
- const fs = require('fs');
- const os = require('os');
- const { parse } = require('date-fns');
- // Node.js script that moves the user's last downloaded file (Mac OSX)
- // to their current working directory.
- const downloadsPath = path.resolve(os.homedir(), 'Downloads');
- fs.readdir(downloadsPath, (err, files) => {
- if (err) return console.log('error found');
- const res = files.map((filename) => {
- const filepath = path.resolve(downloadsPath, filename);
- const { ctime, mtime } = fs.statSync(filepath);
- return { filename, filepath, lastChanged: parse(ctime), lastModified: parse(mtime) };
- });
- const mostRecentFile = res.reduce((a, b) => a.lastModified > b.lastModified ? a : b);
- const mostRecentFilename = mostRecentFile.filename;
- const mostRecentFilepath = mostRecentFile.filepath;
- fs.rename(mostRecentFilepath, path.resolve(__dirname, mostRecentFilename), err => console.log(err));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement