Skip to content

How to delete all pictures of an directory

I am trying to delete all pictures of an directory. But getting error on directory path. And also dont know how to get all pictures path & delete all of them.

My directory structure :

server
  -> app.js
tmp
  -upload
  -- pic.jpg
  -- pic2.jpg
  -- pic3.jpg

I have tried this :

var dir = require('../tmp/upload');
var fs = require('fs');
var promise = require('bluebird');
fs.readdir(dir).then(function(file) {
    console.log(data)

}).catch(function(err){
    console.log
})

But getting error : Cannot find module ‘../tmp/upload’

Need help to get the path & all pictures on upload folder & delete them.

Thanks in advance

Answer

You got this error simply because you actually required a module from the relative path instead of resolving it. In order to resolve a relative path to an absolute path, you need to use path.resolve, not require.

var path = require('path');
var dir = path.resolve('../tmp/upload');