FindFirstFile fails on the root path
Asked Answered
A

1

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?

Astrogation answered 13/2, 2014 at 0:22 Comment(5)
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.Tussis
@JonathanPotter: Thanks. But then I need to know if my path is a root or not. And that creates an additional problem...Astrogation
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.Astrogation
@c00000fd: look at PathIsRoot()Replevin
IIRC, root directories can also be detected by their lack of a . entry (as Jonathan pointed out, root dirs are special)Dalesman
M
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.

Mahau answered 18/2, 2014 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.