简体   繁体   中英

Delete all files and folder except

My function is:

function Delete(dir){
    log(`[DELETE] ${dir}.`)
    fs.readdir(dir, (err, files) => {
    if (err) {
        console.log(err);
    }

    files.forEach(file => {
        const fileDir = path.join(dir, file);

        if (file !== 'specialfile.txt') {
           fs.rm(fileDir, { recursive:true }, (err) => {
        if(err){
        // File deletion failed
        console.error(err.message);
        return;
        }
        })
        }
    });
});

But he only preserves the "specialfile.txt" not the content file in case i like preserver a list of files inside in "specialfile.txt".

specialfile.txt:

error.dll
Windows.txt
jose.cfg

i need exclude all files and folder except this file content;

try using fs.unlink . fs.unlink will not work on a directory, empty or otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM