|
|
Searching, Opening Files in Win32Win32 API Tutorial Example Searching Files in Folders with WildcardsTwo ways to list files based on a wildcard search of a folder so tht they can be opened one by one, using Windows Win32 API calls and C programming.
This is one of the most common programming problems that software application developers are asked to solve. The underlying question is:
There are essentially two steps to the solution:
The solution requires that the following are known in advance: The path There are also two possible solutions to listing the files. The first is a slightly out-of-the-box approach, and essentially revolves around creating a hidden listbox and passing it the LB_DIR message to list files in a folder. There are some advantages to this approach:
For those who want to continue with this method, there is a separate article about it : Storing Data in Hidden List Boxes. A complete discussion of using list boxes in general is in the Programming List Box Windows API article. This solution uses the following Windows API functions and data types:
One last point, for users of Visual ++ (or VC++ Express) : make sure that the project character set is set to a multi-byte character representation in the compile options. (See this article for more details). The SolutionThe trick with looking for files using the Windows API is to remember that it is like any other search function:
The first call can fail because nothing is found. The repetitive call is a different function, and can fail when there is nothing left to find. When everything is finished, the handle to the search process must be closed. This sounds unnecessarily complex, and the Win32 API SDK help documentation can be less than clear. Luckily, FindFirstFile itself is easy to use: WIN32_FIND_DATA myFileData; HANDLE hSearch; // Start search for .txt files: hSearch = FindFirstFile("*.txt", &myFileData); To search another folder, there are two solutions:
The HANDLE that is returned is needed for calls to FindNextFile. So, with some error checking code, the actual loop could look like the following: while (hSearch != INVALID_HANDLE_VALUE) { // read from file // test for more files if (FindNextFile(hSearch, &myFileData) == 0) break; // stop when none left } At each step in the loop, the file can be opened with code similar to: FILE * hFile = fopen((char *) myFileData.cFileName, "rt"); // open file for (r)eading, (t)ext format Finally, it is necessary to close the search handle with FindClose(hSearch). Error CheckingThe GetLastError() function can be used to test for the specific error that caused FindNextFile to return 0 in the loop. It will usually return ERROR_NO_MORE_FILES when there are no more files left. Other than that, the search handle remains valid once created, so can not be used for error checking beyond the first call.
The copyright of the article Searching, Opening Files in Win32 in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Searching, Opening Files in Win32 in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|