(haven't tried on windows but maybe it's similar).
/cc @Araq if you agree with these I can send a PR to fix posix
EDIT
// package com.javatutorialhq.java.examples;
import java.io.File;
public class test1 {
public static void main(String[] args) {
File file = new File(".foo");
boolean result = file.isHidden();
System.out.println(file + " isHidden: " + result);
}
}
//prints: .foo isHidden: true
python: doesn't seem in stdlib but accepted stackoverflow answer behaves same as I recommend:
https://stackoverflow.com/questions/7099290/how-to-ignore-hidden-files-using-os-listdir
C++ : doesn't seem in stdlib/boost but accepted answer does same as I suggest:
bool isHidden(const bf::path &p)
{
bf::path::string_type name = p.filename();
if(name != ".." &&
name != "." &&
name[0] == '.')
{
return true;
}
return false;
}
isHidden("a/.foo") => true
Heh, I don't know. Probably you're right and it should care about paths in general.
As is often the case, it's good to ask this question: how do other languages handle this?
@dom96 I edited my answer above to add what I could find from other languages (couldn't find much lang that support isHidden); everything I found is 100% consistent with my proposal.
Regarding whether it should apply to both files and dirs, that's just common sense.
Regarding whether it should work (on posix) on paths that don't exist, that's also common sense since only string is accessed to determine that, and that's what all answers I could find did anyway (on posix)
@dom96 It's surprising that this isn't in more stdlibs considering it's a bit of a pain to handle across systems. Yes it's fairly easy on posix but Windows requires attribute checks. I really appreciate that this is included.
Should dots be taken into account even on Windows though? Maybe optionally? Lots of dot files are not technically hidden when they probably should be.
@citycide @dom96 @Araq
how about the following:
ospaths.isDotPath => checks only for paths, works cross platform
os.isHidden => calls ospaths.isDotPath on posix, calls windows specific on windows (ie, same as my top level recommendation)
Should dots be taken into account even on Windows though? Maybe optionally? Lots of dot files are not technically hidden when they probably should be.
Never ever. Our stdlib is not responsible for fixing half-assed Unix ports to Windows.
os.isDotPath would not be enough: I am pretty sure that on Linux files that end in ~ are hidden as well.
If the OS exposes a way to figure out whether a file or directory is hidden, that should be used (what happens on windows anyway). I am not sure whether something like that exists on Linux, though
@andreaferretti
os.isDotPath would not be enough: I am pretty sure that on Linux files that end in ~ are hidden as well.
just tried, doesn't seem like that's correct:
total 0
-rw-r--r-- 1 timothee 0 Jul 11 02:06 foo~
-rw-r--r-- 1 timothee 0 Jul 11 02:06 foo~2
prompt:lhmbp /private/tmp/d01 0.020 $ ls -a
total 0
drwxr-xr-x 5 timothee 160 Jul 11 02:06 ./
drwxrwxrwt 28 root 896 Jul 11 02:06 ../
-rw-r--r-- 1 timothee 0 Jul 11 02:06 .bar
-rw-r--r-- 1 timothee 0 Jul 11 02:06 foo~
-rw-r--r-- 1 timothee 0 Jul 11 02:06 foo~2
likewise on OSX.
It seems you're right. Most file managers hide those as well, though
Don't add any new procs. Just implement your proposal.
Most helpful comment
Don't add any new procs. Just implement your proposal.