How to get the first file with a .txt extension in a directory with nodejs?

后端 未结 5 1037
感动是毒
感动是毒 2021-02-20 14:06

The directory all my files are in is: \'/usr/home/jordan\' and I have many files under there (in the directory itself, but one file that is named with a .txt extension.

5条回答
  •  长情又很酷
    2021-02-20 14:15

    var files = fs.readdirSync(homedir);
    var path = require('path');
    
    for(var i in files) {
       if(path.extname(files[i]) === ".txt") {
           //do something
       }
    }
    

提交回复
热议问题