find file with wild card matching

后端 未结 5 1708
滥情空心
滥情空心 2020-12-08 18:31

In node.js, can I list files with wild card matching like

fs.readdirSync(\'C:/tmp/*.csv\')?

I did not find the information on wild card ma

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 18:50

    dont reinvent the wheel, if you are on *nix the ls tool can easily do this (node api docs)

    var options = {
      cwd: process.cwd(),
    }
    require('child_process')
    .exec('ls -1 *.csv', options, function(err, stdout, stderr){
      if(err){ console.log(stderr); throw err };
      // remove any trailing newline, otherwise last element will be "":
      stdout = stdout.replace(/\n$/, '');
      var files = stdout.split('\n');
    });
    

提交回复
热议问题