zoukankan      html  css  js  c++  java
  • nodeschool.io 5

    ~~ FILTERED LS ~~

    Create a program that prints a list of files in a given directory,
    filtered by the extension of the files. You will be provided a
    directory name as the first argument to your program (e.g.
    '/path/to/dir/') and a file extension to filter by as the second
    argument.

    For example, if you get 'txt' as the second argument then you will
    need to filter the list to only files that end with .txt.

    The list of files should be printed to the console, one file per line.
    You must use asynchronous I/O.

    ----------------------------------------------------------------------
    HINTS:

    The `fs.readdir()` method takes a pathname as its first argument and a
    callback as its second. The callback signature is:

    function (err, list) { ... }

    where `list` is an array of filename strings.

    Documentation on the `fs` module can be found by pointing your browser
    here:
    C:UsersdzhangAppDataRoaming pm ode_moduleslearnyounode ode_api
    tml

    You may also find the standard JavaScript `RegExp` class helpful here.

    我写的:

    var fs = require('fs');
    var patt1=new RegExp(".dat");
    var fliter = fs.readdir(process.argv[2],function(err,list){
        for(var i = 0 ; i < list.length ; i++){
    
            if(list[i].split(".").length > 1){
                if(patt1.exec(list[i])){
                    console.log(list[i]);
                }
            }
        }
    })

    网站的答案:

      var fs = require('fs')
      var regex = new RegExp('\.' + process.argv[3] + '$')
    
      fs.readdir(process.argv[2], function (err, list) {
        list.forEach(function (file) {
          if (regex.test(file))
            console.log(file)
        })
      })
  • 相关阅读:
    【洛谷p1309】瑞士轮
    【洛谷p1190】接水问题
    KMP算法小记
    【洛谷p1051】谁拿了最多奖学金
    【洛谷p1781】宇宙总统
    【6.12校内test】T2 子集
    【6.12校内test】T3 城市交通费
    【6.12校内test】T1单词序列
    【洛谷p1464】 Function
    IOS基础之 (十二) 类的扩展
  • 原文地址:https://www.cnblogs.com/della/p/3425623.html
Copyright © 2011-2022 走看看