0

Java question.

What method should I use to find out if a Windows file is marked as content indexed or not? I've tried to write a solution similair to Determine whether a file is a junction (in Windows) or not? , I would like to stress that the following code was written with simply assuming that the .getClass().getDeclaredMethod method did have "isIndexed" as an argument, in line with "isReparsePoint", however, that assumption turns out to be incorrect since the following code always returns as false. I decided to leave it in just in case someone knows the proper reference that would fit this code.

boolean isIndexed = false;
if (DosFileAttributes.class.isInstance(attr)) {
    try {
         Method m = attr.getClass().getDeclaredMethod("isIndexed");
         m.setAccessible(true);
         isIndexed = (boolean) m.invoke(attr);
    } catch (Exception e) {
         // just gave it a try
    }
}

Instead of "isIndexed" in the getDeclaredMethod argument I've also tried using "isContentIndexed" and "isNotContentIndexed", all without any satisfactory results.

Pt. Terk
  • 462
  • 4
  • 13
  • Why do you think that a method named `isIndexed` exists? I see no reason that it would? Where did you find documentation that it exists? – David Heffernan Oct 12 '15 at 11:07
  • First you need to understand the code which you copy'n'paste. There is no such method `isIndexed`, that at least is the explanation why it's not working. For the second part: The only thing might be to use some Windows system calls via JNI, JNA, etc. – SubOptimal Oct 12 '15 at 11:08
  • 1
    @DavidHeffernan I don't. I gave it a try. – Pt. Terk Oct 12 '15 at 11:10
  • Well, I'm going to stop contributing because my comments are being deleted. – David Heffernan Oct 12 '15 at 11:21
  • @DavidHeffernan I'm sorry to hear that, David. I didn't report you or anything, and I can certainly understand why you would laugh at me for guessing a method name for such an obscure function. – Pt. Terk Oct 12 '15 at 11:27
  • @SubOptimal I found a reference for hex codes of different Windows file attributes: https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 ; Should I just import the JNA library and use the code in http://stackoverflow.com/a/3286732/3101380 substituting 0x400 (code for reparse points) with 0x2000 (code for not content indexed)? – Pt. Terk Oct 12 '15 at 11:29
  • I wasn't laughing at you. It was just finding it hard to believe that you really had just made up a method name from guesswork. You really should state that in the question, or better still remove the code and simply ask how to do what you want to do. The code is misleading us. – David Heffernan Oct 12 '15 at 11:30
  • @DavidHeffernan: It was me who flagged your comment as "not constructive", well, because IMHO it was exactly that: OP has given the statement that he just gave it a try and there was no need to express espesial astonishment. The discussion IMHO does not benefit from the deleted comment. Sorry if my behavior was not correct. – Thomas Weller Oct 12 '15 at 11:39
  • @DavidHeffernan I apologize, misleading you was not my intent. I have edited the initial post to clarify that the code was written on a faulty assumption, but I left it in just in case there is actually a correct reference that would fit that code. – Pt. Terk Oct 12 '15 at 11:40
  • @Thomas I was just trying to establish the facts. For all I know, there really is such a method. I don't like to assume. – David Heffernan Oct 12 '15 at 11:42
  • @Peterson I think you should forget about Java for now. Find out how to do this with raw Windows API. Then map back to Java. – David Heffernan Oct 12 '15 at 11:43

1 Answers1

0

The sun.nio.fs.WindowsFileAttributes class does not have any methods to report whether the file is content-indexed or not. You can, however, call the attributes() method to retrieve the underlying bitmask of file attributes, and then check whether the FILE_ATTRIBUTE_NOT_CONTENT_INDEXED (0x2000) attribute bit is set or not:

boolean isIndexed = false;
if (DosFileAttributes.class.isInstance(attr)) {
    isIndexed = true;
    try {
         Method m = attr.getClass().getDeclaredMethod("attributes");
         m.setAccessible(true);
         int attrs = (int) m.invoke(attr);
         isIndexed = ((attrs & 0x2000) == 0);
    } catch (Exception e) {
         // just gave it a try
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • As always, `sun.*` classes should be considered subject to change without notice in future versions of Java. The same goes for private methods. – VGR Oct 12 '15 at 23:45
  • True. The alternative is to use JNA to call the Win32 `GetFileAttributes()` or `FindFirstFile()` functions directly. – Remy Lebeau Oct 13 '15 at 00:50