This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  File Enumerator
  Submitted by



This is a simple but useful little function I wrote for a utility I needed. There is already a similar piece of code submitted by Sam McGrath which I had thought I would use to use but it had a few problems.

First it didn't allow the provision of parameters to supply to the function pointers which was a bit restrictive as I didn't want nasty globals everywhere. Secondly, despite having a flag for recursing directories this didn't work. Infact the recurse variable isn't even used in the code despite being documented in the description!

Because of both of these I wrote a new version, and thought I'd submit it as it is quite a useful function to have lying around.

Andrew Grant -
HotHouse Creations


Download Associated File: enumfiles.cpp (3,111 bytes)

Editor's note:  
COTD Entry:  File Enumeration by Andrew Grant [AndrewGrant@hothouse.org]

#include <windows.h> #include <stdio.h>

// Function name : EnumerateFiles // Description : Takes an initial directory and calls the provided function pointers for each file or directory // which it contains and which matches the specified wildcard search. Replace dirCallback or // fileCallback with NULL to ignore these types. To enumerate all files and directories use *.* // // // Argument : const char *pDirectory - directory to begin searching. do NOT specify a trailing slash! // Argument : const char* pWildCard - wildcard to specify files, e.g *.txt. use *.* for all fiels/directories // Argument : bool recurse - check sub folders? // Argument : void(*fileCallback) - callback for each directory which matches the wildcard // Argument : void* pFileParam - param to be provided to the directory callback functon // Argument : void(*dirCallback) - callback for each directory which matches the wildcard // Argument : void* pDirParam - param to be provided to the directory callback functon void EnumerateFiles(const char *pDirectory, const char* pWildCard, bool recurse, void(*fileCallback)(const char *pFilename, const char *pFilePath, void* pFileParam), void* pFileParam, void(*dirCallback)(const char *pDirname, const char *pDirPath, void* pDirParam), void* pDirParam) {

WIN32_FIND_DATA finddata; HANDLE handle; char searchPath[256];

// first search for files matching the wildcard value sprintf(searchPath, "%s\\%s", pDirectory, pWildCard);

handle = FindFirstFile(searchPath, &finddata);

if (handle != INVALID_HANDLE_VALUE) { // check all files and directories do { if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // ignore directories with "." and ".." if (strcmp(finddata.cFileName, ".") && strcmp(finddata.cFileName, "..")) { if (dirCallback) { dirCallback(finddata.cFileName, pDirectory, pDirParam); } } } else { if (fileCallback) { fileCallback(finddata.cFileName, pDirectory, pFileParam); } }

} while (FindNextFile(handle, &finddata));

FindClose(handle); }

// now repeat for all directories if the recurse flag was specified if (recurse) { sprintf(searchPath, "%s\\*.*", pDirectory);

handle = FindFirstFile(searchPath, &finddata);

if (INVALID_HANDLE_VALUE == handle) return;

do { // only interested in proper directories... if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(finddata.cFileName, ".") && strcmp(finddata.cFileName, "..")) { // call ourself on this dir sprintf(searchPath, "%s\\%s", pDirectory, finddata.cFileName);

EnumerateFiles(searchPath, pWildCard, true, fileCallback, pFileParam, dirCallback, pDirParam); } }

} while (FindNextFile(handle, &finddata));

FindClose(handle); }

return; }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.