F
F
Flakelf2018-10-10 09:08:08
Node.js
Flakelf, 2018-10-10 09:08:08

How to delete all folders in a loop?

I do not buy how to delete folders, fs.rmdir can be applied to one directory, but how to delete all directories in the path, for example, there is a full path to the file \uploads\io\qw\q\we\qw\rt\3449c9e5e332f1dbb81505cd739fbf3f.jpg , I delete the file using the fs.unlink method, everything is OK, and then \uploads\io\qw\q\we\qw\rt\ remains and I try to delete the folders in a loop, but for some reason it does not work. I delete by splitting the string spliit('/"), remove the last element with .pop(), then I convert the array to a string with join('/') and try to delete with fs.rmdir(), everything is in a loop, but it does not work .

let path = 'uploads/io/io/ui/yu/rt/q/'.split('/');
              lastEl;
 
            for (var i = path.length - 1; i > 0; i--) {
 
                path = path.join('/');
                console.log(path);
 
                fs.rmdir(path, (err) => {
                    console.log('Удалена директория ' + path);
                });
 
                console.log('Удалена директория ' + path);
 
                path = path.split('/');
                lastEl = path.pop();
                // console.log(oldPhotoPath);
 
            }
 
/* Удалена директория uploads/io/io/ui/yu/rt/q
uploads/io/io/ui/yu/rt
Удалена директория uploads/io/io/ui/yu/rt
uploads/io/io/ui/yu
Удалена директория uploads/io/io/ui/yu
uploads/io/io/ui
Удалена директория uploads/io/io/ui
uploads/io/io
Удалена директория uploads/io/io
uploads/io
Удалена директория uploads/io
Удалена директория uploads
Удалена директория uploads
Удалена директория uploads
Удалена директория uploads
Удалена директория uploads
Удалена директория uploads */

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question