node.js remove file

前端 未结 17 1507
误落风尘
误落风尘 2020-12-07 06:54

How do I delete a file with node.js?

http://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback

I don\'t see a remove command?

17条回答
  •  半阙折子戏
    2020-12-07 07:46

    Remove files from the directory that matched regexp for filename. Used only fs.unlink - to remove file, fs.readdir - to get all files from a directory

    var fs = require('fs');
    const path = '/path_to_files/filename.anyextension'; 
    
    const removeFile = (fileName) => {
        fs.unlink(`${path}${fileName}`, function(error) {
            if (error) {
                throw error;
            }
            console.log('Deleted filename', fileName);
        })
    }
    
    const reg = /^[a-zA-Z]+_[0-9]+(\s[2-4])+\./
    
    fs.readdir(path, function(err, items) {
        for (var i=0; i

提交回复
热议问题