5

I'm using the following code to obtain information about a file system directory:

LPCTSTR pStrPath = L"D:\\1";
WIN32_FIND_DATA wfd;
HANDLE hDummy = ::FindFirstFile(pStrPath, &wfd);
if(hDummy != INVALID_HANDLE_VALUE)
{
    //Use 'wfd' info
    //...

    ::FindClose(hDummy);
}
else
{
    int error = ::GetLastError();
}

The code works just fine, unless I specify a root path:

  • D:\ - error code ERROR_FILE_NOT_FOUND
  • D: - error code ERROR_FILE_NOT_FOUND
  • \\SRVR-1\share - error code ERROR_BAD_NET_NAME
  • \\SRVR-1\share\ - error code ERROR_BAD_NET_NAME
  • \\SRVR-1\HiddenShare$ - error code ERROR_BAD_NET_NAME

But it works in the following cases:

  • D:\1 - no error
  • \\SRVR-1\share\1 - no error
  • \\SRVR-1\HiddenShare$\1 - no error

Any idea why?

5
  • 1
    The root of a device isn't a normal directory entry, so FindFirstFile can't return information about it. You can use calls like GetVolumeInformation and GetDiskFreeSpaceEx to find out various things about a volume. Feb 13, 2014 at 0:26
  • @JonathanPotter: Thanks. But then I need to know if my path is a root or not. And that creates an additional problem...
    – c00000fd
    Feb 13, 2014 at 0:28
  • 1
    OK. I found that at least I can use GetFileAttributesEx for the attributes. That API doesn't seem to treat root path differently from any other path.
    – c00000fd
    Feb 13, 2014 at 0:46
  • 3
    @c00000fd: look at PathIsRoot() Feb 13, 2014 at 0:54
  • IIRC, root directories can also be detected by their lack of a . entry (as Jonathan pointed out, root dirs are special)
    – MSalters
    Jun 29, 2015 at 15:08

1 Answer 1

7

FindFirstFile() is meant to be used to enumerate the contents of a directory. As such it is meant to be used with a file pattern, such as D:\*.

When you use D:\1 you are just using a very restrictive file pattern (1) to filter the files in D:\, but when you use just D:\ or D: there is no pattern at all!

And the same is true for shared resources. Note that \\SRV-1\share does not count as a pattern, because \\SRV-1 cannot be considered a directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.