FindFirstFile

This is a discussion on FindFirstFile within the Windows Programming forums, part of the Platform Specific Boards category; Below is a short code I've extracted from the FAQ on "Viewing files in a directory". But I have a ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    FindFirstFile

    Below is a short code I've extracted from the FAQ on "Viewing files in a directory".
    But I have a small problem with the file type. I'm only looking for mp3 files, but if do *.mp3 it can't locate sub directories... How would you suggest for me to do this, I thought about strstr() but it seems like a really lame idea.

    Thanks in advance.
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void doit(char *root)
    {
      HANDLE h;
      WIN32_FIND_DATA info;
      char newroot[MAX_PATH];
    
      SetCurrentDirectory(root);
      if ((h=FindFirstFile("*.mp3", &info)) != INVALID_HANDLE_VALUE)
      {
        do
        {
          if (!(strcmp(info.cFileName, ".") == 0 || strcmp(info.cFileName, "..") == 0))
          {
             if (info.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
             {
                sprintf(newroot, "%s\\%s", root, info.cFileName);
                doit(newroot);
                SetCurrentDirectory("..");
             }
             else
                cout << info.cFileName << "\n";
          }
        } while (FindNextFile(h, &info));
        FindClose(h);
      } 
    }
    
    int main()
    {
       doit("c:\\dir1\\dir2");
       return(0);
    }
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> I thought about strstr() but it seems like a really lame idea.

    Er, why? You get all data, you check if it's a directory and you recurse it, otherwise, you check if it's an mp3, and if it is, do whatcha want wittit.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Tonto
    >> I thought about strstr() but it seems like a really lame idea.

    Er, why? You get all data, you check if it's a directory and you recurse it, otherwise, you check if it's an mp3, and if it is, do whatcha want wittit.
    Well that's just it, if I'm using *.mp3 instead of *.* it doesn't return anything that doesn't end with mp3, which means that I don't see the sub directories.
    The only way I managed to get a list of the sub directories is with *.*.

    As I said before, I'm thinking of using *.* and strstr() to determine the file type.
    But I'm afraid of weird looking files like: file.txt.obj.exe.com.mp3
    You never know how weird people can get.

    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Devil Panther
    But I'm afraid of weird looking files like: file.txt.obj.exe.com.mp3
    You never know how weird people can get.
    Using *.* with FindFirstFile/FindNextFile would also turn up results such as 'file.txt.obj.exe.com.mp3'. It will turn up anything with a '.' in it.

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Quantum1024
    Using *.* with FindFirstFile/FindNextFile would also turn up results such as 'file.txt.obj.exe.com.mp3'. It will turn up anything with a '.' in it.
    What I mean is how to detect that the file is actually mp3, and not something else.

    Allow me to explain myself.
    Let say the directory contains 3 files: a.txt b.mp3 c.txt.mp3
    Using the *.* I will see both, but how will I know which one is the mp3?!
    I was thinkg about using the strstr() function to detect ".mp3".
    But strstr() function will only return the first detected value, in file C: .txt
    even though the file is really mp3.
    Basically a reverse strstr() function can give me the answer I'm looking, to scan the string from the end instead of the begining.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263
    You mean like:-
    Code:
    if (!strcmp(&info.cFileName[lstrlen(info.cFileName) - 4], ".mp3"))
    {
        // Good to go
    }
    ?

    NOTE: You would have to check that info.cFileName is at least 4 chars long first, otherwise it's booboo time.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Just a little note in addition, you may want to do a string comparison that is not case sensitive.

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by mercury529
    Just a little note in addition, you may want to do a string comparison that is not case sensitive.
    Good point, thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    One more thing please.
    One of the sub directory's dwFileAttributes is 17, but the directory value is 16: FILE_ATTRIBUTE_DIRECTORY

    I've checked all the possible values, but non of them equels to 17, how can I find out what is this value?

    FILE_ATTRIBUTE_ARCHIVE
    FILE_ATTRIBUTE_COMPRESSED
    FILE_ATTRIBUTE_DIRECTORY
    FILE_ATTRIBUTE_ENCRYPTED
    FILE_ATTRIBUTE_HIDDEN
    FILE_ATTRIBUTE_NORMAL
    FILE_ATTRIBUTE_OFFLINE
    FILE_ATTRIBUTE_READONLY
    FILE_ATTRIBUTE_REPARSE_POINT
    FILE_ATTRIBUTE_SPARSE_FILE
    FILE_ATTRIBUTE_SYSTEM
    FILE_ATTRIBUTE_TEMPORARY
    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > but non of them equels to 17,
    You OR them together
    Like 17 could be

    FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN

    Well, you get the idea, find out which attribute has the value 1

    Which would of course mean a hidden directory.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FindFirstFile() Always Returns INVALID_HANDLE_VALUE
    By ThLstN in forum Windows Programming
    Replies: 10
    Last Post: 09-05-2008, 12:12 PM
  2. FindFirstFile() and std::string
    By Mostly Harmless in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2008, 06:27 PM
  3. FindFirstFile
    By Coding in forum C# Programming
    Replies: 8
    Last Post: 02-13-2008, 03:49 AM
  4. how do i use FindFirstFile()
    By knight543 in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2002, 07:00 PM
  5. Searching for a file using FindFirstFile() ?
    By Brian in forum Windows Programming
    Replies: 7
    Last Post: 01-27-2002, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21