Delete several files in node.js

后端 未结 8 1443
自闭症患者
自闭症患者 2021-02-15 23:58

What is the best way to delete several files in node.js?

function deleteFiles(files, callback){
    ...
}

var files = [\'file1.js\', \'file2.jpg\', \'file3.css\         


        
8条回答
  •  伪装坚强ぢ
    2021-02-16 00:06

    You can use this to delete multiple files (for linux env ES6 syntax)

    import {execSync} from "child_process";
    ...
    
    ...
    const files = ['file1.js', 'file2.jpg', 'file3.css'];
    // just example path
    const rootDir = '/var/www/files/';
    if (files.length > 0)
        execSync('rm -f' + files.map(o => rootDir + o.replace(/(\s+)/g, '\\$1')).join(' '));
    

    rm -f to ignore if files are missing and replace to escape spaces if file names contains it,

提交回复
热议问题