Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

I need to find all files with a same extension on a drive...

Anyone could provide me an algorithm to do that?

Something that covers all the folders starting from the root?

Thanx.


Comments

  • Here's a complete prog. Will find jpg files in the drive you want, I've used the Win32 API FindFirstFile/FindNextFile, you can convert to C's findfirst/findnext easy if you want to..



    #include "windows.h"

    #include "stdio.h"



    void search_folder( const char *pszFolderName, const char *pszExt )

    {

    // add *.* to folder name to search for all files and folders

    char szBuffer[ _MAX_PATH ];

    wsprintf( szBuffer, "%s\*.*", pszFolderName );



    // find first file

    WIN32_FIND_DATA wfd;

    HANDLE h = FindFirstFile( szBuffer, &wfd );

    if( h != INVALID_HANDLE_VALUE )

    {

    do

    {

    // check if its a folder

    if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )

    {

    // skip . and .. or else we'd recurse to hell

    if( lstrcmpi( wfd.cFileName, "." ) && lstrcmpi( wfd.cFileName, ".." ) )

    {

    // build new folder name and search it

    wsprintf( szBuffer, "%s\%s", pszFolderName, wfd.cFileName );

    search_folder( szBuffer, pszExt );

    }

    }

    else

    {

    // its a file, check extension

    char szTempExt[ _MAX_PATH ];

    _splitpath( wfd.cFileName, NULL, NULL, NULL, szTempExt );

    if( lstrcmpi( (char*) ( szTempExt + 1 ), pszExt ) == 0 )

    {

    // got one, build full file name and show it

    wsprintf( szBuffer, "%s\%s", pszFolderName, wfd.cFileName );

    printf( "%s
    ", szBuffer );

    }

    }

    }

    while( FindNextFile( h, &wfd ) ); // find next file



    CloseHandle( h );

    }

    }





    main()

    {

    search_folder( "c:", "jpg" );



    return 0;

    }





    : Anyone could provide me an algorithm to do that?

    : Something that covers all the folders starting from the root?

    : Thanx.

    :






Sign In or Register to comment.