Nodejs - Check for hidden files

后端 未结 2 1563
心在旅途
心在旅途 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:58

    Did some quick testing using node 0.6.x on Windows 7. The setup was a folder containing 1 folder, 1 protected, 1 hidden and 1 file without special attributes.

    I looped this folder and fetched the stats for the entries (using fs.stat(path, callback)), these are the results:

    testfolder
    fs.Stats.mode: 16895
    
    test_hidden.txt
    fs.Stats.mode: 33206
    
    test_norm.txt
    fs.Stats.mode: 33206
    
    test_prot.txt
    fs.Stats.mode: 33060
    

    As you can see, one is able to differ between protected and hidden/normal files through the mode, but the hidden attribute is actually a real attribute and has nothing to do with the file mode.

    In order to reliably identify hidden files on Windows, the node.js team would have to implement the GetFileAttributes() API on windows (like it's done by C++ or C#). AFAIK, this is not in the pipeline (at least i found nothing after some quick googling).

    For your question concerning files being hidden in all flavors of unix when prefixed by a period: i didn't come across a distribution where this didn't work, so from my pov: yes.

提交回复
热议问题