FindFirstFile trouble

This is a discussion on FindFirstFile trouble within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I'm trying to access the file names of all the files in a folder. I am trying to use ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    FindFirstFile trouble

    Hi. I'm trying to access the file names of all the files in a folder. I am trying to use FindFirstFile to do this. However, I'm having some issues.

    Code:
    void TestFunction()
    {	
    	string directoryPath = "C:\\TestFolder\\";
    
    	WIN32_FIND_DATA FindFileData;
    	HANDLE hFind = INVALID_HANDLE_VALUE;
    
    	hFind = FindFirstFile(directoryPath.c_str(), &FindFileData);	
    	//cout << FindFileData.cFileName << endl;
    
    	if(hFind != INVALID_HANDLE_VALUE)
    	{
    		cout << FindFileData.cFileName << endl;
    		cout << "Test" << endl;
    	}
    	else
    	{
    		cout << "Fail" << endl;
    	}
    }
    Currently, "Fail" will print. If I try to output the file name before the if statement, it prints jibberish. I've looked over a bunch of examples, but I can't seem to figure out what the problem is.
    In this current example, I had to change my project settings to "Use Multi-Byte Character Set" instead of "Use Unicode character set". If I leave it as "Use Unicode character set", I change the following in my code:

    Code:
    hFind = FindFirstFile(LPCWSTR(directoryPath.c_str()), &FindFileData);
    This will also print "Fail". If I output the file name before the if statement, it outputs: 0012FC18

    Any ideas?
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    >>hFind = FindFirstFile(LPCWSTR(directoryPath.c_str()), &FindFileData);
    This is wrong on so many levels. Don't do it. Setting project settings to multi-byte is correct.

    As for the problem... try appended "*.*" to your string.
    If it doesn't work, remember to get the error code by calling GetLastError to see what went wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    That fixed it. I guess I still had it set to the Unicode setting when I added the astrisk the first time.

    Thanks for your help.
    IDE - Visual Studio 2005
    Windows XP Pro

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want all files in the folder, you need to do something like this...

    Code:
    void EnumFiles(TCHAR *Path)
      { WIN32_FIND_DATA  da;   // directory data
         HANDLE  ds;     // directory handle
         SetCurrentDirectory(Path);
         ds = FindFirstFile(_TEXT("*.*"),&da);
         do
           { // do what you need with the da record here
    
            } while( FindNextFile(ds,&da) != ERROR_NO_MORE_ITEMS);
         FindClose(ds);  }
    There's an internal index that is reset to 0 each time you call FindFirstFile() and it's incremented each time you call FindNextFile() so unless you do it in a loop, you'll only get the first filename ( a dot ) each time.
    Last edited by CommonTater; 04-26-2011 at 01:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FindFirstFile
    By Coding in forum C# Programming
    Replies: 8
    Last Post: 02-13-2008, 04:49 AM
  2. FindFirstFile
    By Devil Panther in forum Windows Programming
    Replies: 10
    Last Post: 11-24-2006, 12:23 PM
  3. FindFirstFile problem
    By XX@nnX in forum Windows Programming
    Replies: 7
    Last Post: 08-23-2006, 08:02 AM
  4. FindFirstFile()
    By caduardo21 in forum Windows Programming
    Replies: 7
    Last Post: 02-14-2005, 07:14 AM
  5. how do i use FindFirstFile()
    By knight543 in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2002, 08:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21