Hi
I am looking for a simple program that goes thru the directory tree to find a file. No MFC and other stuff, just plain Win32 development.
Thnx a lot
This is a discussion on simplest file search within the Windows Programming forums, part of the Platform Specific Boards category; Hi I am looking for a simple program that goes thru the directory tree to find a file. No MFC ...
Hi
I am looking for a simple program that goes thru the directory tree to find a file. No MFC and other stuff, just plain Win32 development.
Thnx a lot
Under File Management Functions:
FindFirstFile()
Under Path Functions:
PathFindFileName
PathFindOnPath()
Of course, you may have to use these functions recursively (or in a loop) to search subdirectories. Here is a code snippet from a simple find function I created:
Code:/* calling routine */ ... /* call find on some directory */ find("C:\some\directory\to\start\from"); ... ... void find(const char *rootPath) { char searchTerm[strlen(rootPath)+3]; strcpy(searchTerm, rootPath); strcat(searchTerm, "\*"); WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(searchTerm, &fd); if(hFind == INVALID_HANDLE_VALUE) return; do { /* if its a subdirectory, call find again */ if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(fd.cFileName , ".")!=0 && strcmp(fd.cFileName , "..")!=0) { char subPath[strlen(rootPath) + strlen(fd.cFileName) + 2]; strcpy(subPath, rootPath); strcat(subPath, "\"); strcat(subPath, fd.cFileName); find(subPath); } else { /* do some other stuff its a file */ } } while(FindNextFile(hFind, &fd)); FindClose(hFind); }
You mean like the many examples in the FAQ then![]()
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Thanks.
A small question:
will char searchTerm[strlen(rootPath)+3];
compile? Should be a constant expression, shouldn't it?
ole
Yes it should be a constant expression.
Last edited by Salem; 04-09-2007 at 12:32 AM. Reason: too vague
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.