/* dirparse.c * Richard Heathfield * Code to parse a directory tree in Win32. */ #include #include #include "windows.h" #include "io.h" int ParseDirectoryTree(const char *Path) { int Status = 0; HANDLE FileHandle = {0}; WIN32_FIND_DATA FileData = {0}; char FullPath[MAX_PATH] = {0}; char SubDirPath[MAX_PATH] = {0}; sprintf(FullPath, "%s\\*.*", Path); fprintf(stderr, "Checking path %s\n", FullPath); FileHandle = FindFirstFile(FullPath, &FileData); if(INVALID_HANDLE_VALUE == FileHandle) { printf("Error in FindFirstFile for %s\n", FullPath); } else { do { if(strcmp(FileData.cFileName, ".") != 0 && strcmp(FileData.cFileName, "..") != 0) { if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { sprintf(SubDirPath, "%s\\%s", Path, FileData.cFileName); Status = ParseDirectoryTree(SubDirPath); } else { printf("%s\\%s\n", Path, FileData.cFileName); } } } while(FindNextFile(FileHandle, &FileData)); FindClose(FileHandle); } return Status; } int main(int argc, char **argv) { if(argc < 2 || access(argv[1], 0)) { puts("Usage: dirparse path"); puts("e.g. dirparse c:\alldata"); puts("Produces a directory listing"); puts("of all files, including files in"); puts("subdirectories."); } else { ParseDirectoryTree(argv[1]); } return 0; }