Thread: simplest file search

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    simplest file search

    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

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    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);
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    Thanks.

    A small question:
    will char searchTerm[strlen(rootPath)+3];
    compile? Should be a constant expression, shouldn't it?

    ole

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by ole111 View Post
    Thanks.
    A small question:
    will char searchTerm[strlen(rootPath)+3];
    compile? Should be a constant expression, shouldn't it?
    It's fine if you're using a C99 compliant compiler (which is pretty much any decent compiler).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM