Nodejs - Check for hidden files

后端 未结 2 1566
心在旅途
心在旅途 2021-02-19 11:30

I\'m iterating a directory of files and was wondering if it\'s possible to test if a file is hidden or not. Currently, I\'m just checking if file starts with a \'.\' or not. Thi

2条回答
  •  你的背包
    2021-02-19 11:36

    The regular expression to effectively detect hidden files and directory path in Unix would be a bit more complex owing to the possibility of their existence within a long path string.

    The following tries to take care of the same.

    /**
     * Checks whether a path starts with or contains a hidden file or a folder.
     * @param {string} source - The path of the file that needs to be validated.
     * returns {boolean} - `true` if the source is blacklisted and otherwise `false`.
     */
    var isUnixHiddenPath = function (path) {
        return (/(^|\/)\.[^\/\.]/g).test(path);
    };
    

提交回复
热议问题