我正在迭代文件目录,想知道是否可以测试文件是否隐藏。目前,我只是检查文件是否以“.”开头或不。这适用于 Mac(也许还有 Linux),但是,我想知道如何在 Windows 上做到这一点?另外,句点会在所有版本的 Linux 中隐藏该文件吗?
Thanks!
Code:
var fs = require('fs');
fs.readdir('/path', function(err, list) {
list.forEach(function(filename){
var isHidden = /^\./.test(filename);
// etc ...
});
});
在 Unix 中有效检测隐藏文件和目录路径的正则表达式会稍微复杂一些,因为它们可能存在于长路径字符串中。
下面尝试解决同样的问题。
/**
* 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);
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)