views:

125

answers:

1

I am using the Windows API function FindFirstFileEx because it provides the capability to return just the sub-directories of a given directory (ignoring files). However when I call this function with the required flag, I still receive both files and directories.

The MSDN documentation for the FindExSearchLimitToDirectories flag used by FindFirstFileEx says:

This is an advisory flag. If the file system supports directory filtering, the function searches for a file that matches the specified name and is also a directory. If the file system does not support directory filtering, this flag is silently ignored.

The lpSearchFilter parameter of the FindFirstFileEx function must be NULL when this search value is used.

If directory filtering is desired, this flag can be used on all file systems, but because it is an advisory flag and only affects file systems that support it, the application must examine the file attribute data stored in the lpFindFileData parameter of the FindFirstFileEx function to determine whether the function has returned a handle to a directory.

So, what file systems actually support this flag? It would have been sensible to actually list these supported file systems on the same page, but I can't find it.

My development system is Windows XP SP3, NTFS, .NET 3.5.

I know I can check file attributes to determine if a file is a directory, however this means checking the every file/directory. It also defeats the purpose of using FindFirstFileEx in the first place.

Of course there is still the chance I may be doing something incorrectly in my code. The only thing I can see is passing IntPtr.Zero to lpSearchFilter may not be the same as passing NULL (as mentioned in the quote).

Here's an example of the code I'm using:

    m_searchDirHandle = WinAPI.FindFirstFileEx(@"C:\Temp\*",
       WinAPI.FINDEX_INFO_LEVELS.FindExInfoStandard , 
       ref m_findDirData, WinAPI.FINDEX_SEARCH_OPS.FindExSearchLimitToDirectories,
       IntPtr.Zero , 0);

    if (m_searchDirHandle != WinAPI.INVALID_HANDLE_VALUE)
    {
        do
        {
            foundNextDir = WinAPI.FindNextFile(m_searchDirHandle, ref m_findDirData);

        } while (foundNextDir);
    }
+3  A: 

The nearest link I could find was, the list of System Calls by Metasploit...I am taking a stab here but I would imagine that this 'FindFirstFileEx' would somehow be an indirect call to the NT system call equivalent 'NtOpenDirectoryObject', 'NtQueryDirectoryFile', 'NtQueryDirectoryObject'... I hope...if anyone thinks I'm wrong and downvotes to disagree, I will be corrected by whoever disagrees :)

However, I have hit on a few links here

  • CodeGuru forum on this issue about the flag
  • Wine has a mailing listed as the flag as no effect?
  • GenNT mentions that it is apparently limited to NTFS, (there's 3 replies to that posting)
  • Here on SO, a question on 'How to get list of folders in this folder'

Edit: Just now after mentioning in the comments, I thought it would be fitting enough to add a link to the Linux NTFS driver for capabilities to read the NTFS partition, there is bound to be source version changes to accomodate the different NTFS versions going back to Win2000...

Hope this helps, Best regards, Tom.

tommieb75
Also, its possible that this depends on which _version_ of ntfs you are using. I believe there were some changes around the WinXP time frame.
John Knoeller
@John: I would be inclined to agree since there was NTFS changes (from Vista onwards in relation to the new bootloader method) and also, XP home and professional had one crucial difference in the NTFS - security, in professional, you could set the permissions, not available under Home...
tommieb75
Please see my edited answer... :)
tommieb75