Search code examples
javafileruntimeexechidden

Why my file doesnt become hidden with Runtime Attrib +H (java)


I have a File 'f' that I've just created, and I want to make it Hidden, so I use the following code:

Runtime.getRuntime().exec("attrib +H "+f.getCanonicalPath());

but it doesnt work in all cases, it appears to work only on famous folders like 'Desktop' folder, 'Documents' folder .. but on random folders like 'Desktop/randomFolder' it doesnt make the file Hidden.

Someone knows why and how can I solve this? The application is for Windows. Sorry for bad english.


Solution

  • I find a way to solve it by doing the following:

    String a[] = {"attrib","+H",f.getCanonicalPath()};
    Runtime.getRuntime().exec(a);
    

    I'd to separate all the parts of my command into a string array 'a'. Then, I use it on 'exec()' function.

    Sorry for bad english again.