• 0

[C++] FindFirstFile


Question

I can't figure out how to get FindFirstFile working.

 sr = FindFirstFile("E:\Documents and Settings\*",0);
  if(sr != INVALID_HANDLE_VALUE)
    {
      users.insert(iter,"Hello");
    }
    else
    {
      cout << "No users detected! Exiting...";
      return 0;
    }

(yes I am aware it's currently only looking at the E drive)

As you've probably guessed, I am trying to find the dcouments folders for each user. However, the routine never finds any files and gives me the error message. I can'tfigure out how to use WIN32_FIND_DATA to specify folders, which may be the problem. Any suggestions?

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Well first, you're not going to get back a list of anything using a single call to FindFirstFile. You call FindFirstFile once, and then call FindNextFile repeatedly until you get back an invalid handle.

Second, try using *. instead of * for your search if you're looking for directories (this will also return files without an extension, but there shouldn't be any of those in the Documents and Settings folder).

Link to comment
Share on other sites

  • 0

Yeah, I know about FindNextFile but I think getting the seach working in the first place is more important! :p

*. has no effect, as I thought. There aren't any files in there!

Link to comment
Share on other sites

  • 0

try '\\' and not a single '\' & remove the *.

so, instead of "E:\Documents and Settings\"

try, "E:\\Documents and Settings\\"

The * is being used inside a string literal and therefore it may actually be searching for a folder called '*'

Of course, I haven't even looked at FindFirstFile, so I doubt I understand how it works properly.

Link to comment
Share on other sites

  • 0

That's the one, thanks! I never knew about double-slashes. :)

This is what I now have:

 FindFirstFile("E:\\Documents and Settings\\",0);
  sr = FindNextFile;
  if(sr != INVALID_HANDLE_VALUE)
    {
      users.insert(iter,[b]"test"[/b]);
      FindClose(sr);
    }
    else
    {
      cout << "No users detected! Exiting...";
      return 0;
    }

It doesn't come up with the error message, but I now need to replace "test" with the search results held in sr. What do I need to do to extract that?

Edited by Varsity
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.