dimanche 29 septembre 2019

Selecting random file but then deleting it

I have some code that selects a random file from a folder then logs it, after its done logging I want it to be deleted but I can't seem to delete it.

The path to the randomly selected file is stored as a variable.

const trash = require('trash');
var fs = require('fs'),
    path = require('path'),
    files_memes = fs.readdirSync('/Users/jackb/Desktop/pup/Pictures/reddit_sub_memes')
var filename = (random_file());
var imgPATH = path.join(__dirname, '/Users/jackb/Desktop/pup/Pictures/reddit_sub_memes/' + filename);
const del = require('del');

function random_file(){
  var allFiles = (files_memes)//array
  return allFiles[Math.floor(Math.random() * allFiles.length)];
}
console.log(filename);
console.log(imgPATH);
(async () => {
    const deletedPaths = await del(['imagePATH']);

    console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
})();

I expect the logged file to be deleted.




Aucun commentaire:

Enregistrer un commentaire