Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. const path = require('path');
  2. const fs = require('fs');
  3. const os = require('os');
  4. const { parse } = require('date-fns');
  5.  
  6. // Node.js script that moves the user's last downloaded file (Mac OSX)
  7. // to their current working directory.
  8.  
  9. const downloadsPath = path.resolve(os.homedir(), 'Downloads');
  10.  
  11. fs.readdir(downloadsPath, (err, files) => {
  12. if (err) return console.log('error found');
  13.  
  14. const res = files.map((filename) => {
  15. const filepath = path.resolve(downloadsPath, filename);
  16. const { ctime, mtime } = fs.statSync(filepath);
  17. return { filename, filepath, lastChanged: parse(ctime), lastModified: parse(mtime) };
  18. });
  19.  
  20. const mostRecentFile = res.reduce((a, b) => a.lastModified > b.lastModified ? a : b);
  21. const mostRecentFilename = mostRecentFile.filename;
  22. const mostRecentFilepath = mostRecentFile.filepath;
  23.  
  24. fs.rename(mostRecentFilepath, path.resolve(__dirname, mostRecentFilename), err => console.log(err));
  25. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement